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
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