Hey,
I have a Problem with my GUI. When I touch the Brick the Gui appear but when i try it on the 2. Time its don`t work :(
Script Inside the Brick:
--Variables local Object = script.Parent.Parent.Parent.Parent.Objects:FindFirstChild(script.Parent.Parent.Object.Value) local Cooldown = false local Cooldown_Time = 5 --OnTouch Function function onTouched(hit) if Cooldown == false then Cooldown = true local Player = game.Workspace:FindFirstChild(hit.Parent.Name) if Player then local plr = game.Players:FindFirstChild(Player.Name) print(plr.Name) print(plr.leaderstats.Cash.Value) print(script.Parent.Parent.Price.Value) if plr.leaderstats.Cash.Value >= script.Parent.Parent.Price.Value then script.Parent.Transparency = 1 else plr.PlayerGui.Error.Frame:TweenPosition(UDim2.new(0.5, -343,0.5, -204)) local Cash = script.Parent.Parent.Price.Value - plr.leaderstats.Cash.Value plr.PlayerGui.Error.Frame.TextLabel.Text = "You need "..Cash.." more $ to buy "..Object.Name wait(Cooldown_Time) Cooldown = false end end end end --Function script.Parent.Touched:connect(onTouched)
I hope somebody can help me :D
To debug this, you should print out any relevant variables. For example, your if
statement says "don't do anything unless Cooldown is false". So why not print out what Cooldown is before that if statement runs? I believe you would discover that it becomes true and then stays there. In that case, you could then print out all the places where you change the value of Cooldown
. If the script doesn't set Cooldown to false, why not? You can continue to add print statements until you understand what it's doing and how to solve it (which I discuss below).
Miscellaneous notes:
plr
and Player
. (If what you're doing is consistently capitalizing variables that are "global" to the script, like Cooldown/Object/etc, what about onTouched
? But so long as you have a system, that's okay.)local player = game.Players:PlayerFromCharacter(hit.Parent)
and then check to see if player
exists. There might theoretically be multiple things in the workspace with the name of the player, meaning you might get the wrong model/player object when using FindFirstChild
Cooldown
to true, you can safely do if {shouldn't continue} then return end
, ex: if Cooldown then return end
-- this enables you to not indent the rest of your function.FindFirstChild
for Object
. If you are confident the object will be there, you should use square brackets to get the child name: script.Parent. ... .Objects[script.Parent.Parent.Object.Value]
or else you should use WaitForChild
instead of FindFirstChild
.Solution: The problem is that you should only ever set Cooldown
to true right before the wait
. If any of the if
statements after Cooldown = true
fail (ie the player doesn't exist or they have enough cash), your script will never run Cooldown = false
.
Script fixed (except capitalization in case that's how you prefer it):
--Variables local Object = script.Parent.Parent.Parent.Parent.Objects:FindFirstChild(script.Parent.Parent.Object.Value) local Cooldown = false local Cooldown_Time = 5 function onTouched(hit) if Cooldown then return end local plr = game.Players:PlayerFromCharacter(hit.Parent.Name) if not plr then return end print(plr.Name) print(plr.leaderstats.Cash.Value) print(script.Parent.Parent.Price.Value) if plr.leaderstats.Cash.Value >= script.Parent.Parent.Price.Value then script.Parent.Transparency = 1 else Cooldown = true plr.PlayerGui.Error.Frame:TweenPosition(UDim2.new(0.5, -343,0.5, -204)) local Cash = script.Parent.Parent.Price.Value - plr.leaderstats.Cash.Value plr.PlayerGui.Error.Frame.TextLabel.Text = "You need "..Cash.." more $ to buy "..Object.Name wait(Cooldown_Time) Cooldown = false end end script.Parent.Touched:connect(onTouched)
Notice how I moved Cooldown = true
to right before any yielding functions (like TweenPosition or wait
), ensuring that Cooldown
will never get stuck at true
as it did before (unless your code errors).