I am making 3d 1 Dollar bill in Roblox Studio, and there is a problem with cloning it. I made a touched event connected to the dollar and tried attempting to make it so once you step on the bill you gain 1 "Money" for the leader stats. It works all well until the clone, once you step on it you destroy the bill but then after 2 seconds the bill respawns so you can get more money but.. The bill only respawns and it does not function again as the script tells it to. Why is this happening?
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local MoneyCollected = Instance.new("IntValue") MoneyCollected.Name = "Money" MoneyCollected.Parent = leaderstats local OneDollar = game.Workspace.OneDollar local Onedollar = OneDollar:FindFirstChild("Hitbox") local OneDollarC = OneDollar:Clone() local OnedollarC = Onedollar:Clone() Onedollar.Touched:Connect(function(hit) player.leaderstats.Money.Value = player.leaderstats.Money.Value + 1 Onedollar:Destroy() OneDollar:Destroy() wait(2) OneDollarC.Parent = game.Workspace -- puts it in the workspace but the script doesn't work anymore, why? end)
end)
-SqmetImesDev
You need to connect the .Touched event to the OneDollar that is cloned.
Try this:
local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local MoneyCollected = Instance.new("IntValue") MoneyCollected.Name = "Money" MoneyCollected.Parent = leaderstats local OneDollar = game.Workspace.OneDollar local Onedollar = OneDollar:FindFirstChild("Hitbox") local OneDollarC = OneDollar:Clone() local event local hitFunc = function(hit) player.leaderstats.Money.Value = player.leaderstats.Money.Value + 1 hit.Parent:Destroy() wait(2) local newDollar = OneDollarC:Clone() local newHitbox = newDollar:FindFirstChild("Hitbox") or newDollar:WaitForChild("Hitbox") newDollar.Parent = game.Workspace event:disconnect() event = newHitbox.Touched:Connect(hitFunc) end