Script no longer working after part is cloned to workspace

I have been trying to make a money printer for my game where the printer spawns a money model after the timer counts down and the player can click the money models to add a certain value of currency to their leaderstats.

The script controlling clicking the money and adding value to the leaderstats works fine but I am having issues here:

After the money printer clones my part into the workspace it can no longer be collected. Its worth noting the scripts in question are inside of the money part and the scripts are also being cloned by the money printer

The cash part is in ReplicatedStorage and is being cloned into workspace

I have tried kickstarting the scripts by using the money printer spawning script to change the state of the script from enabled to disabled then back to enabled again but that didnt help

-- Below is the script handling the Cash Parts
local replicatedStorage = game:GetService("ReplicatedStorage")

local cashClicked = script.Parent:WaitForChild("ClickDetector")
local pop = game.ReplicatedStorage.Sounds.pop
local collected = game.ReplicatedStorage.Sounds.Cash


local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()

local canClick = true
local cash = player:WaitForChild("leaderstats").Cash
local cashValue = 250

cashClicked.MouseClick:Connect(function(player)
	print("cash clicked")
	if canClick == true then
		local highlight = Instance.new("Highlight")
		highlight.Parent = script.Parent
		local highlightEffect = game:GetService("TweenService"):Create(highlight, TweenInfo.new(1), {OutlineTransparency = 1, FillTransparency = 1})
		highlight.FillColor = Color3.fromRGB(0, 207, 0)
		highlightEffect:Play()

		highlightEffect.Completed:Connect(function()
			game.Debris:AddItem(highlight)
		end)
		canClick = false
		collected:Play()
		cash.Value += cashValue
		game.Debris:AddItem(script.Parent, 0.2)
		task.wait(0.2)
		--pop:Play()
		canClick = true
	end
end)

And then here are the scripts handling the money printer

local maxCount = 8
local startingTime = 10

local moneyPrinter = script.Parent.Parent
moneyPrinter.TimeLeft.Seconds.Value = startingTime

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local partToClone = ReplicatedStorage:WaitForChild("Cash"):WaitForChild("GreenBill")
local referencePart = workspace:WaitForChild("MoneyPrinter")



function Timer(minutes, seconds, timerLabel)
	repeat
		task.wait(1)
		if seconds <= 0 then
			minutes -= 1
			seconds = 59
		else
			seconds -= 1
		end
		if seconds < 10 then
			timerLabel.Text = tostring(minutes).. ":0" ..tostring(seconds)
		else
			timerLabel.Text = tostring(minutes).. ":"..tostring(seconds)
		end
	until minutes <= 0 and seconds <=0
	
	local cashObject = partToClone:Clone()
	cashObject.Parent = workspace
	cashObject:WaitForChild("CashClickScript").Enabled = false
	cashObject:WaitForChild("CashClickScript").Enabled = true
	
	local distance = 5
	
	cashObject.CFrame = referencePart.CFrame * CFrame.new(0, 0, -distance)
end

for count = 0, maxCount do
	print("loop")
	print(count)
	if moneyPrinter:IsA("Part") then
		local timeLeft = moneyPrinter:WaitForChild("TimeLeft")
		local minutes = timeLeft:WaitForChild("Minutes")
		local seconds = timeLeft:WaitForChild("Seconds")
		local timerLabel = moneyPrinter:WaitForChild("TimerGui"):WaitForChild("TextLabel")

		Timer(minutes.Value, seconds.Value, timerLabel)
	end
end

--[[if moneyPrinter:IsA("Part") then
	local timeLeft = moneyPrinter:WaitForChild("TimeLeft")
	local minutes = timeLeft:WaitForChild("Minutes")
	local seconds = timeLeft:WaitForChild("Seconds")
	local timerLabel = moneyPrinter:WaitForChild("TimerGui"):WaitForChild("TextLabel")

	Timer(minutes.Value, seconds.Value, timerLabel) 
end]]

first of all it’s not the best way to have one script in every part. You can try to use Collection Service for this.

Why u disable and enable script? This might cause why u can’t reach your cash part

1 Like

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local cash = player:WaitForChild(“leaderstats”).Cash
cash.Value += cashValue

Why u using local script to add money for player? It’s wouldn’t change for other players. Or if it is a server script that this wouldn’t work. Local player doesn’t exist for server side scripts

also found out this line

If you would need to change cashValue it wouldn’t be easy. Use Module Sctipts
they can work as storage for your game. Like that:

local MoneyData = {
	["CoinName1"] = {
		["CashVal"] = 250,
		["TimeToRegen"] = 12,
		["IdkSomeOtherStuff"] = true
	}
}

return MoneyData

hope this will help you. If you can’t understand sth lmk :).