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

Chest Script Gives Infinite Money Instead of Set Amount?

Hi, this chest script works by being touched, giving the player their gold and sending the chest to Lighting as a way to make it despawn for 45 seconds. The thing is, this script works when picking one up one at a time or small amounts of chests are being used in the map. However, when there are lots of players are on and there are lots of chests, the chests will break and be able to be touched and give money without despawning into Lighting, giving players infinite money basically. When I tried to assign the chests random numbers in their name so the script knows which specific chest to put back into position, this solution did not end up working.

What could be the problem here?

amnt = math.random(50,100)
chest = script.Parent
chest.Name = ("Chest"  .. math.random(0,500))



function onTouched(part)
	local h = part.Parent:findFirstChild("Humanoid")
	if (h~=nil) then
		local thisplr = game.Players:findFirstChild(h.Parent.Name)
		if (thisplr~=nil) then
			local stats = thisplr:findFirstChild("leaderstats")
			if (stats~=nil) then
				local score = stats:findFirstChild("Gold")
				if (score~=nil) then
					score.Value = score.Value + amnt
					print("Touch")
					chest.Parent = game.Lighting
					wait(45)
					game.Lighting:FindFirstChild(chest.Name).Parent = game.Workspace
				end
			end
		end
	end
end

script.Parent.Special.Touched:connect(onTouched)

2 comments

3

Use a debounce.

0

You should use GetPlayerFromCharacter not findFirstChild from players. It is very simple: local thisplr = game.Players:GetPlayerFromCharacter(h.Parent)

1 answer

The problem is that touching something most often touches it dozens of times at once, since your character is moving while touching it. I think a better way to handle this is by having it give you the amount when you Click on it rather than touch it.

2 comments

0

True, however after those dozen touches the chest still disappears. For me this is not the case, The players can stand on the chest and infinitely collect from it without it ever dissappearing

0

If the server lags you can still click it more than once until the chest disappears.

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