Simple script doesn't work properly

I made a script that gives the player wood. But sometimes it gives more than one wood. And I know why this is happening, but I have no idea how to fix it.

local rs = game:GetService("ReplicatedStorage")
local rm = rs.ToInv
local plrname = ""
local players = game:GetService("Players")
script.Parent.Touched:connect(function(hit)
	
	script.Parent.Position = Vector3.new(0, 0, 0)
	print("touched")
	
	if hit.Parent:FindFirstChild("Humanoid") then
		
		print("human found")
		local player = players:GetPlayerFromCharacter(hit.Parent)
		if not player then return end
		print(player.Name)
		rm:FireClient(player, "wood")
		wait(60)
		script.Parent.Position = Vector3.new(-8.144, 22.073, 166.029)
	else
		script.Parent.Position = Vector3.new(-8.144, 22.073, 166.029)
	end
end)



local rs = game:GetService("ReplicatedStorage")
local rm = rs.ToInv
local inv = script.Parent.InvFrame.Inv
rm.OnClientEvent:Connect(function(item)
	print("remote event fired")
	if item == "wood" then
		for i = 0, 13 do
			local istr = tostring(i)
			if inv:WaitForChild("Slot" .. istr).item.Value == "" then
				inv:WaitForChild("Slot" .. istr).item.Value = "wood"
				
				break
			end
		end
	end
end)

How can I fix this?

1 Like

Would you mind elaborating? What is supposed to be ‘Wood’? A currency, or so?

1 Like

Maybe try adding a debounce, as sometimes .Touched events fire multiple times, creating your issue.

3 Likes

Thanks, I didn’t think it would be that simple!

2 Likes