So after touching the part and getting 'minutes' aka the currency I want the script to break so it won't be able to get any more minutes from the part. Anyone know how? I tried to; then break, break, wait(seconds) break, etc and it won't work.
THE SCRIPT;
script.Parent.Touched:Connect(function(hit) local debounce = true if debounce then debounce = false if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then local player = game.Players[hit.Parent.Name] player.leaderstats.Minutes.Value += 1 end debounce = false end end)
The problem is:
script.Parent.Touched:Connect(function(hit) local debounce = true --When the function starts, the debounce is set to true if debounce then --So this part is useless, because it's set to true in the prev. line debounce = false if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then local player = game.Players[hit.Parent.Name] player.leaderstats.Minutes.Value += 1 end debounce = false end end)
You can do:
local debounce = true --I moved it up, so it won't be to true every time the function starts script.Parent.Touched:Connect(function(hit) if debounce then debounce = false if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then local player = game.Players[hit.Parent.Name] player.leaderstats.Minutes.Value += 1 end end end)
Or you can Destroy the part when it's touched:
script.Parent.Touched:Connect(function(hit) if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then local player = game.Players[hit.Parent.Name] player.leaderstats.Minutes.Value += 1 script.Parent:Destroy() --This will delete the part permanently end end)
Let me know if it helped
Well, I can suggest you a better method, but it can be only executed once per server. It can be done by using Disconnect. Later, I will also fix your debounce
, so it won't error if you try using debounce
next time.
Advantages of Disconnect
: You can still add code to your script, and make it run (if not connected with Touched event). It just makes a connected function disconnect but won't affect the rest of the code (out of that function).
The simple fix :
local connection -- Creating a nil variable connection = script.Parent.Touched;Connect(function(hit) -- Setting that variable to the function if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- It is a better way to get the player player.leaderstats.Minutes.Value += 1 connection:Disconnect() -- Disconnects our functions and makes it never to be used again end end)
Now, here's the script fixing your debounce
:
local debounce = true -- MAKE SURE, DEBOUNCE SHOULD BE OUT OF THE FUNCTION THAT YOU ARE APPLYING ON script.Parent.Touched:Connect(function(hit) if debounce then debounce = false if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then local player = game.Players[hit.Parent.Name] player.leaderstats.Minutes.Value += 1 end -- debounce = false [This line is useless, as you have already set your debounce to false] end end) -- Well, the error might happen, when another part touches this part, due to which, Touched event will fire and setting of your debounce to false. The result will be, if player touches it after another non-humanoid part have touched it, then the value won't increase.
Lemme know if it helps!