Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can seem to make a script wait before doing it again and destroy it?

Asked by
Shematics 117
5 years ago
Edited 5 years ago

Hi i have my Cash Giving script, I tried to make it so it get destroyed when touched and can't seem to remove the model.Plus it just give money but spam it, I can't figure out why!

function onTouched(hit)
if hit.Parent.Humanoid~= nil then
local cash = game.Players:findFirstChild(hit.Parent.Name).leaderstats.Cash
wait(1)
cash.Value = cash.Value + 1
script.Parent:Destroy()
wait(5)
end
script.Parent.Touched:connect(onTouched)

1 answer

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

We need to set up a variable so that the function will only run once

local ran=false
function onTouched(hit)
if ran==false and hit.Parent.Humanoid~= nil then ran=true
local cash = game.Players:findFirstChild(hit.Parent.Name).leaderstats.Cash
wait(1)
cash.Value = cash.Value + 1
script.Parent:Destroy()
wait(5)
end
script.Parent.Touched:connect(onTouched)
0
Does not seem to work.. Shematics 117 — 5y
Ad

Answer this question