Script not running when cloned from lighting to workspace?

Hey DevForum! I’ve been trying to learn scripting (since I have tons of time to, because of lockdown) and I’ve been trying to make a basic money system where if I touch a money part, it adds, let’s say, 10 to my Money leaderstat.

Although for some reason, it only works when the part is already in Workspace. When it’s cloned to Workspace from lighting, it just doesn’t work. Nothing happens. No error in the output, either.
I’m probably missing something very obvious, but I can’t tell what it is.

The script is disabled by default when in Lighting, then enabled when cloned. What am I doing wrong?

Here’s my code:

(The code used when touching the money part.)

money = script.Parent

money.Touched:connect(function(Hit)
	if Hit and Hit.Parent then
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		local moneyStat = Player.leaderstats.Money
		print("Hit")
		moneyStat.Value = moneyStat.Value + 10
	end
end)

(An excerpt from the script that’s used to create the part when this function is called, the cloning part works, the giving money part doesn’t as described up there. Will provide more if needed.)

local function CreatePart()
	local money = game.Lighting.moneyPart:Clone()
	local moneyScript = money.Script
	money.Parent = game.Workspace
	money.Position = Vector3.new(Character.HumanoidRootPart.Position.X,Character.HumanoidRootPart.Position.Y+100,Character.HumanoidRootPart.Position.Z)
	moneyScript.Disabled = false
end
1 Like

Is the .Archivable property set to true? Of the part.

I think it is. Should it be disabled?

It should be enabled, but that is the most common issue with people not being able to :Clone().

Also, why are you cloning it from Lighting? You should be storing things in ServerStorage.

Try putting the part in ReplicatedStorage or ServerStorage, I don’t think it’s a good idea to save parts in Lighting.

I tried putting it in ReplicatedStorage and then cloning from there to workspace. That didn’t fix it.

Then I tried the same thing with ServerStorage. It doesn’t even get cloned.

Is it a server or local script?

Server script, I tried it with a local script and it didn’t even run at all.

Can you send a screenshot of your explorer tab? Include everything that the script deals with, including the script itself.

Sure.

There is no reason to store a script inside the part when you can just make the event connection when you create the part.

And please, please learn to use ServerStorage. Lighting is for lighting stuff, not for storage.

local function CreatePart()
	local money = game.Lighting.moneyPart:Clone()
	local moneyScript = money.Script
	money.Parent = game.Workspace
	money.Position = Vector3.new(Character.HumanoidRootPart.Position.X,Character.HumanoidRootPart.Position.Y+100,Character.HumanoidRootPart.Position.Z)
	money.Touched:Connect(funtion(Hit)
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		local moneyStat = Player.leaderstats.Money
		print("Hit")
		moneyStat.Value = moneyStat.Value + 10
    end)
end
1 Like

Have the script in Workspace or ServerScriptService. Not inside the part.

I tried doing this, and didn’t work. Nothing happened.

EDIT: NEVERMIND, I’m just REALLY stupid. Managed to get it working myself.

The script ended up erroring when I tried this.
And sure, I’ll try learning to use ServerStorage properly soon.

EDIT: NEVERMIND, I’m just REALLY stupid. Managed to get it working myself.