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

Script doesn't destroy first hit, but destroys the rest?[CLOSED]

Asked by
u_g 90
10 years ago

I'm working on a tycoon machine, and I discovered that the part that will give money once touched does not destroy the first part it detects once touched. In other words, the first part is not destroyed, but once it is gone, the rest are destroyed. Here is the script:

local debounce = false
script.Parent.Touched:connect(function(hit)
if debounce == true then return end
if debounce == false then
debounce = true
local tycoonowner = script.Parent.Parent.Parent.TycoonOwner
local player = game.Players:GetChildren()
for i = 1, #player do
if player[i].Name == tycoonowner.Value then
player[i].leaderstats.Money.Value = player[i].leaderstats.Money.Value + script.MoneyAwarded.Value
hit:Destroy()
end
if player[i].Name ~= tycoonowner.Value then

end
end
wait(1)
debounce = false
end
end)

1 answer

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

You have so many unneeded things in there. Try this:

local tycoonownerVal = script.Parent.Parent.Parent.TycoonOwner
local money = script.MoneyAwarded

script.Parent.Touched:connect(function(hit)
    hit:Destroy()
    local tycoonowner = game.Players:FindFirstChild(tycoonownerVal.Value)
    if tycoonowner then
        tycoonowner.leaderstats.Money.Value = tycoonowner.leaderstats.Money.Value + money.Value
    end
end)

Tell me if this is not what you wanted.

0
Thanks. It worked great. u_g 90 — 10y
Ad

Answer this question