Why is the tool cloning?

So this dude in this topic

SO I used 2 of his posts the one above and if u click it and scroll up and it worked, but when it gives me the tool it gives me 2 instead of 1. someone please help :frowning:

1 Like

Can we see your script so we can identify the issue?

1 Like
game.ReplicatedStorage.ToolPurchase.OnServerEvent:Connect(function(player) --Runs when the remote event is fired, change 'ToolPurchase' to the name of the remote event if you changed it
	local clonedTool = script.Parent.Teddy:Clone() --Clones the tool, change 'Tool' to the name of the tool and change script.Parent if it is in a different location.
	clonedTool.Parent = player.Backpack --Puts it in the player's backpack, 'player' being the parameter automatically passed through which tells us who fired the event.
end)
script.Parent.MouseButton1Click:Connect(function() --Detects for when a player clicks the Gui
	script.Parent.Rotation = math.random(-9,9)
	script.Parent.Visible = false
	wait(0.001)
	script.Parent.Parent.Teddy.Visible = true
	wait(2)
	game.ReplicatedStorage.ToolPurchase:FireServer() --Fires the remote event (change ToolPurchase to the name of the remote event if it is named something different)
end)

And I have a remote event everything works but it just gives me 2 tools instead of one

script.Parent.Touched:Connect(function(hit) --Event fires when the part is tocuhed
	if hit.Parent:FindFirstChild("Humanoid") then --Confirms it is a character
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Finds the player
		if player.leaderstats.Cash.Value >= 50 then --Checks for the right amount, change 'Cash' to the name of your stat
			game.StarterGui:WaitForChild("ScreenGui").Enabled = true --Change ScreenGui to the Gui name
			game.StarterGui.ScreenGui.Parent = player.PlayerGui --Inserts the Gui into the player so only they can see it
		end
	end
end)

Hiya! I recommend you using a debounce variable, like:

local debounce = false
game.ReplicatedStorage.ToolPurchase.OnServerEvent:Connect(function(player) 
    if debounce == false then debounce = true --Runs when the remote event is fired, change 'ToolPurchase' to the name of the remote event if you changed it
    local clonedTool = script.Parent.Tool:Clone() --Clones the tool, change 'Tool' to the name of the tool and change script.Parent if it is in a different location.
    clonedTool.Parent = player.Backpack --Puts it in the player's backpack, 'player' being the parameter automatically passed through which tells us who fired the event.
    debounce = true
end) 

Hope this helped ya! :slight_smile:

Maybe check to see if there is a tool already in the player’s backpack? Like so:

game.ReplicatedStorage.ToolPurchase.OnServerEvent:Connect(function(player)
if player.Backpack:FindFirstChild("Teddy") then
-- Object is there, do nothing
return
else
	local clonedTool = script.Parent.Teddy:Clone() 
	clonedTool.Parent = player.Backpack 
end
end)

Yeah that didn’t work either. : rip

1 Like
break statement must be inside a loop

says that.

Change it to return, instead of break.

Okay, I will give you a tip.

[OFFTOPIC] Since exploiters can spam remote events, NEVER TRUST THE CLIENT.

copy the code from the script that listens to the remote event, and put it in that script.
Example:

script.Parent.MouseButton1Click:Connect(function() --Detects for when a player clicks the Gui
	local clonedTool = script.Parent.Tool:Clone() --Clones the tool, change 'Tool' to the name of the tool and change script.Parent if it is in a different location.
	clonedTool.Parent = player.Backpack --Puts it in the player's backpack, 'player' being the parameter automatically passed through which tells us who fired the event. 
end)

Hope it helped you. :slight_smile:

Thank you for helping me!

also thank you @BenMactavsin
for helping too!

edit: Ima Solution this post so I can give Solution button to both of ya :smiley:

1 Like

Thank you! I don’t know why he said break,@BenMactavsin !