I am trying to make a script where when you get money when a package is dropped off on a conveyor. But when the box touches the disposal part it runs more than once even if I have a for I loop. Here is my code:
game.Workspace.Touch.Touched:Connect(function(hit)
script.Parent.ProximityPrompt.Triggered:Connect(function(Player)
local Character = Player.Character
local RedBox = Character:WaitForChild("RedBox")
local DropOff = script.Parent
RedBox.Parent = game.Workspace
RedBox.Handle.CFrame = DropOff.CFrame
end)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Disposal = game.Workspace.RedDisposal.Disposal
local Done = {}
Disposal.Touched:Connect(function(hit)
if hit.Name == "Handle"then
local Handle = hit
Handle:Destroy()
for i = 1,2,1 do
Player.leaderstats.Money.Value += 100
end
end
end)
end)
The more you touch the “Touch” part, the more “Disposal” event will be fired all at once. I suggest putting Disposal.Touched outside of the Touched event on the first part.
local TouchedParts = {}
Disposal.Touched:Connect(function(hit)
if hit.Name == "Handle" and table.find(TouchedParts, hit) == nil then
local pos = #TouchedParts + 1
TouchedParts[pos] = hit
local Handle = hit
Handle:Destroy()
table.remove(TouchedPart, pos)
for = 1, 2 do
Player.leaderstats.Money.Value += 100
end
end
end)
the touch part is a part I used so like that I can get the character which I can reference the player to get the leaderstats and update the money value.
Touch part (invis part for leaderstats)
Disposal (part you need to touch when box gets destroyed)
So you need to place a box on a conveyor then place it down and when it reaches the end, it would destory the box and give money
local Disposal = game.Workspace.RedDisposal.Disposal
Disposal.Touched:Connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent.Parent)
if Player then
if hit.Name == "Handle" then
local Handle = hit
Handle:Destroy()
for i = 1,2,1 do
Player.leaderstats.Money.Value += 100
end
end
end)
local TouchedParts = {}
game.Workspace.Touch.Touched:Connect(function(hit)
script.Parent.ProximityPrompt.Triggered:Connect(function(Player)
local Character = Player.Character
local RedBox = Character:WaitForChild("RedBox")
local DropOff = script.Parent
RedBox.Parent = game.Workspace
RedBox.Handle.CFrame = DropOff.CFrame
end)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Disposal = game.Workspace.RedDisposal.Disposal
local Done = {}
Disposal.Touched:Connect(function(hit)
if hit.Name == "Handle" and table.find(TouchedParts, hit) == nil then
local pos = #TouchedParts + 1
TouchedParts[pos] = hit
local Handle = hit
Handle:Destroy()
table.remove(TouchedPart, pos)
for i = 1, 2 do
Player.leaderstats.Money.Value += 100
end
end
end)
end)
The .Touched event is an event that can fire several times…
You need to add a debounce for this event, otherwise yea the line will be read multiple tmes.