I tried everyway to somehwo make this work... It works on the first time you execute it, but if you try to execute it again it just fails. Here is the code:
script.Parent.MouseButton1Down:Connect(function()
KS.Visible = true
KSIO = true
wait(.5)
UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
if KSIO == true then
script.Parent.Text = Input.KeyCode.Name
KS.Visible = false
KSIO = false
end
print(KSIO)
end)
end)
If you want to disconnect the event, then you can do something like this to disconnect the event.
local conn
function handle(parm1, parm2)
-- logic goes here
if somethingistrue then
conn:Disconnect()
end
end
conn = UIS.InputBegan:Connect(handle)
You want to look at globals. Globals in lua are variables that can be accessed from another thread.
You might be looking for this.
if getgenv().HasRanBefore then return end
if not getgenv().HasRanBefore then getgenv().HasRanBefore = true end
script.Parent.MouseButton1Down:Connect(function()
KS.Visible = true
KSIO = true
wait(.5)
UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
if KSIO == true then
script.Parent.Text = Input.KeyCode.Name
KS.Visible = false
KSIO = false
end
print(KSIO)
end)
end)
Related
Im trying to change to get the zamount's value in the client side but it returns it as nil, can anyone help??
script:
script.Parent.MouseClick:Connect(function(plr)
if plr.Team == game.Teams.Mastermind then
local zombiespawn = script.Parent.Parent
game.ReplicatedStorage.Events:WaitForChild("Onzombiespawnclick"):FireAllClients(plr,script.Parent.Parent.CFrame ,zombiespawn)
end
end)
Local script:
game.ReplicatedStorage.Events:WaitForChild("Onzombiespawnclick").OnClientEvent:Connect(function(spawncframe, zombiespawn)
script.Parent.Visible = true
zombiespawnreference = spawncframe
zombieamountreference = zombiespawn:FindFirstChild("Zombieamount").Value
end)
if zombieamountreference < game.ReplicatedStorage.Vals:WaitForChild("Zombieamountlimit").Value then
if not debounce then
debounce = true
script.Parent:WaitForChild("Placeevent"):FireServer(zombiename, zombiespawnreference, power, zombieamountreference)
wait(0.3)
debounce = false
end
end
Nevermind, i had to put a player object on the client event parameter thing. Its fixed now
I'm trying to make a system to which when the player joins they are given a base and then no other players can use that base.
Here is my module inside the parented to the script:
local Functions = {}
function Functions.findOpenBase(plr)
local bases = workspace.Bases
for i,v in pairs(bases:GetChildren()) do
if v:IsA("Part") then
print("Searching..")
if plr.alreadyOwnsBase.Value == false then
if v.Owner.Value ~= nil then
print("Base found!")
v.Owner.Value = plr.Name
plr.alreadyOwnsBase.Value = true
else
warn("error")
plr:Kick("error finding base, Please Rejoin.")
end
end
else
print("cannot claim another base")
end
end
end
return Functions
then here is my handler script:
local module = require(script.Functions)
game.Players.PlayerAdded:Connect(function(plr)
local alreadyOwnsBase = Instance.new("BoolValue", plr)
alreadyOwnsBase.Name = "alreadyOwnsBase"
alreadyOwnsBase.Value = false
if plr then
module.findOpenBase(plr)
print(plr.Name)
end
end)
is there any solutions?
To me, module inside the parented to the script:
local Functions = {}
function Functions.findOpenBase(plr)
-- check it first.
if plr.alreadyOwnsBase.Value == false then
local bases = workspace.Bases
for i,v in pairs(bases:GetChildren()) do
print("Searching..")
if v:IsA("Part") then
if v.Owner.Value ~= nil then
print("Base found!")
v.Owner.Value = plr.Name
plr.alreadyOwnsBase.Value = true
-- get it, jump the loop
break
end
else
print("cannot claim another base")
end
end
end
-- and plr.alreadyOwnsBase.Value is the flag of the result
if plr.alreadyOwnsBase.Value == false then
warn("error")
plr:Kick("error finding base, Please Rejoin.")
end
end
return Functions
and the handler script:
local module = require(script.Functions)
game.Players.PlayerAdded:Connect(function(plr)
local alreadyOwnsBase = Instance.new("BoolValue", plr)
alreadyOwnsBase.Name = "alreadyOwnsBase"
alreadyOwnsBase.Value = false
if plr then
module.findOpenBase(plr)
-- check the flag
if plr.alreadyOwnsBase.Value then
print(plr.Name)
end
end
end)
For some reason, I think the error messages are telling me to both take away a bracket or add one and I'm very confused. It is always either telling me that there is supposed to be something instead of the bracket or that a bracket is needed depending on how I change it.
Here's the code:
local Ammo = MaxAmmo
local Reloading = false
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Icon = "rbxassetid://1008161057"
local function Reload()
Reloading = true
Mouse.Icon = "rbxassetid://1008161057"
wait(2)
Ammo = MaxAmmo
Reloading = false
Mouse.Icon = "rbxassetid://1008161057"
end
script.Parent.Activated:Connect(function()
if Ammo>0 and not Reloading then
Ammo=Ammo-1
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
if Mouse.Target == ("Head") then
print('head')
Mouse.Target.Parent.Humanoid:TakeDamage(40)
else do
Mouse.Target.Parent.Humanoid:TakeDamage(20)
print('body')
end
end
elseif Reloading == false then
Reload()
end
print(Ammo)
end)
--local Input = game:GetService("UserInputService")
--Input.InputBegan:Connect(function(Key)
--if Key.Keycode == Enum.KeyCode.r and Reloading == false and Ammo~=MaxAmmo then
--Reload()
--end
--end)
end)```
if Mouse.Target == ("Head") then
print('head')
Mouse.Target.Parent.Humanoid:TakeDamage(40)
else do
Mouse.Target.Parent.Humanoid:TakeDamage(20)
print('body')
end
Either add an end to that do or remove it.
To fix such errors count all keywords that require an end and all ends and see if you get the same number. There are text editors that help you with that
You should use an IDE like ZeroBraneStudio, or IntelliJ Community (with EmmyLua)
First issue:
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
if Mouse.Target == ("Head") then
print('head')
Mouse.Target.Parent.Humanoid:TakeDamage(40)
else do
Mouse.Target.Parent.Humanoid:TakeDamage(20)
print('body')
end
end
You should remove do from else do
Second issue:
You'll be getting "unexpected )" from here:
print(Ammo)
end)
This is because you're missing an end from if Ammo > 0 and not Reloading then
The resultant code should look like this:
local Ammo = MaxAmmo
local Reloading = false
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Icon = "rbxassetid://1008161057"
local function Reload()
Reloading = true
Mouse.Icon = "rbxassetid://1008161057"
wait(2)
Ammo = MaxAmmo
Reloading = false
Mouse.Icon = "rbxassetid://1008161057"
end
script.Parent.Activated:Connect(function()
if Ammo > 0 and not Reloading then
Ammo = Ammo - 1
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
if Mouse.Target == ("Head") then
print('head')
Mouse.Target.Parent.Humanoid:TakeDamage(40)
else
Mouse.Target.Parent.Humanoid:TakeDamage(20)
print('body')
end
elseif Reloading == false then
Reload()
end
print(Ammo)
end
end)
--local Input = game:GetService("UserInputService")
--Input.InputBegan:Connect(function(Key)
--if Key.Keycode == Enum.KeyCode.r and Reloading == false and Ammo~=MaxAmmo then
--Reload()
--end
--end)
end)
This script is from Roblox Studio and is in Lua. I have tried using separate scripts but when I do, they don't work because of the bool value. The scripts seem either not to respond to it or it just doesn't update correctly. I have attached the picture of the hierarchy from ROBLOX Studio. I am reasonably to new to Lua but I am familiar with the basic concepts. I also know the Roblox Studio Interface deeply. If anyone could help, that would be greatly appreciated. Thanks in advance.
local lockvalue = script.Parent.Lockdown.Value
lockvalue = false
local RobBank = script.Parent["Rob Bank"]
local ClickDetector = RobBank:WaitForChild("ClickDetector")
local BillboardCXZ = RobBank:WaitForChild("BillboardGui")
local Billboard = BillboardCXZ:WaitForChild("TextLabel")
local emergencylight1 = script.Parent.Parent.EmergencyLight.Toggle.Value
local emergencylight2 = script.Parent.Parent.EmergencyLight2.Toggle.Value
local emergencylight3 = script.Parent.Parent.EmergencyLight3.Toggle.Value
local alarm1 = script.Parent.Parent.Alarm1.AlarmSound
local alarm2 = script.Parent.ParentAlarm2.AlarmSound
local Notif1 = script.Parent.NotifcationScreen.SurfaceGui.Frame.Visible
local Notif2 = script.Parent.NotifcationScreen.SurfaceGui.Frame.TextButton.MouseButton1Click
print ("Values Loaded.")
local function lock()
print ("LOCK FUNCTION ACTIVATED")
emergencylight1 = true
emergencylight2 = true
emergencylight3 = true
alarm1:play()
alarm2:play()
lockvalue = true
Notif1 = true
end
local function unlock()
print ("UNLOCK FUNCTION ACTIVATED")
emergencylight1 = false
emergencylight2 = false
emergencylight3 = false
alarm1.stop()
alarm2:stop()
lockvalue = false
Notif1 = false
end
Notif2:Connect(function()
unlock()
print ("Unlocked Via Override")
end)
RobBank.ClickDetector.MouseClick:Connect(function(Player)
print ("Functioning")
if Player and Player.Character then
print ("Milestone 2")
if lockvalue == false then
print ("After Lock Value")
if Player.Team == game.Teams.Criminal then
print ("Team Check")
local clicks = Player:FindFirstChild("leaderstats")["Bounty"]
clicks.Value = clicks.Value + 500
local clicks2 = Player:FindFirstChild("leaderstats")["Cash"]
clicks2.Value = clicks2.Value + 2500
Billboard.TextColor3 = Color3.new(1,0,0)
lock()
wait(60)
unlock()
Billboard.TextColor3 = Color3.new(0,1,0)
else
print("Player Is not On Crim team.")
Billboard.Text = ("You are on the Wrong Team!")
wait(3)
Billboard.Text = ("Steal Cash")
end
else
Billboard.Text = ("Already been recently robbed!")
wait(3)
Billboard.Text = ("Steal Cash")
end
end
end)
In Lua, a variable can be a reference to a table which has children (key/value pairs):
local myTable = { }
myTable.myKey = true
However, if you refer to a single child (by its key) then it will evaluate and pass only the resulting value into the variable (not a reference to the key itself).
local newVar = myTable.myKey
print(newVar) -- true
newVar = false
print(newVar) -- false
print(myTable.myKey) -- true, because we never changed myKey
So in the following statement, emergencylight1 is probably the value 'true' or 'false' rather than being a reference to the property itself.
local emergencylight1 = script.Parent.Parent.EmergencyLight.Toggle.Value
Recommendation
Ensure your variables are references to an actual table, and then refer to a child of that table (by its key) when assigning a new value.
local emergencylighttoggle1 = script.Parent.Parent.EmergencyLight.Toggle
local emergencylighttoggle2 = script.Parent.Parent.EmergencyLight2.Toggle
local emergencylighttoggle3 = script.Parent.Parent.EmergencyLight3.Toggle
local function lock()
emergencylighttoggle1.Value = true
emergencylighttoggle2.Value = true
emergencylighttoggle3.Value = true
end
local function unlock()
emergencylighttoggle1.Value = false
emergencylighttoggle2.Value = false
emergencylighttoggle3.Value = false
end
i need help
this is the script:
script.Parent.Parent.Activated:connect(function()
local a = game.Workspace.LogRideToggled.Value
if a == true then
a = false
script.Parent.Click:Play()
end
if a == false then
a = true
script.Parent.Click:Play()
end
end)
this is the hirachy:
https://imgur.com/a/4FXHY
but NOTHING happens, no errors either, except for the click sound playing
i seriously need help
The problem is is that after you do a == true, you set a to false, which a == false then matches after.
You can solve this with an if then else end statement, like so:
script.Parent.Parent.Activated:connect(function()
local a = game.Workspace.LogRideToggled.Value
if a == true then
a = false
script.Parent.Click:Play()
else
a = true
script.Parent.Click:Play()
end
end)
However, this will only change the local value of a, which means that it will not save the change.
To fix this, we need to assign the game.Workspace.LogRideToggleds value of Value directly, which we can do like:
script.Parent.Parent.Activated:connect(function()
if game.Workspace.LogRideToggled.Value == true then
game.Workspace.LogRideToggled.Value = false
script.Parent.Click:Play()
else
game.Workspace.LogRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Although it's bad practice to repeatedly index this like this, so we can store game.Workspace.LogRideToggled in a local variable. You can read up on why this works but storing value doesn't here
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value == true then
logRideToggled.Value = false
script.Parent.Click:Play()
else
logRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Also, the == true is redundant, as Lua expects a truthy or falsey value as the condition, all == true does in this case is give true or false if it's true of false.
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value then
logRideToggled.Value = false
script.Parent.Click:Play()
else
logRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Yet we can clean this up a bit more, as we use script.Parent.Click:Play() in both cases, and we can replace logRideToggled.Value = with a logical not, like so.
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value then
-- Todo if truthy
else
-- Todo if falsey
end
logRideToggled.Value = not logRideToggled.Value
script.Parent.Click:Play()
end)
But if you only want to toggle this value, not do anything special for either case, we can remove that entire conditional, leaving:
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
logRideToggled.Value = not logRideToggled.Value
script.Parent.Click:Play()
end)
Hope this has helped!