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:
- (Minor) I recommend consistently using lowerCamelCase for variables - ex you have
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.)
- You should use
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
- It looks like it might just be how it got copied over, but make sure you have properly tabbed/spaced your code so it's clear which blocks of code will be running in different cases
- One style I recommend: if you haven't yet set
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.
- You should not use
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):
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).
3 comments
In the GUI is an "X" Button
I can probally help you, but you need to describe your question more. What errors do you face? What action steps have you tried? Describe your question more and dont just give us code.
So, I'm about to create a custom tycoon kit. I want that if you run a button and has too little money that opens a GUI! It works but only once, so if I try a second time to overflow and again not enough money does not appear GUI