Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

1 comment

0

Thanks. It worked great.

This discussion is preserved as it was. New answers, comments, and votes are closed.