I'm trying to make a tycoon game and I'm trying to change the money value inside the leaderstat folder. But I get errors saying "attempt nil with leaderstat" and I don't know why. Help appreciated.

More info, this is a normal script.

Dropper = workspace.Dropper

script.Parent.ClickDetector.MouseClick:Connect(function()
	local Players = game:GetService("Players")
	if Players.LocalPlayer.leaderstats.Money.Value >= 500 then
		Players.LocalPlayer.leaderstats.Money.Value = Players.LocalPlayer.leaderstats.Money.Value - 500
	else
		return
	end
	for _, v in ipairs(Dropper:GetChildren()) do
		if v:IsA("Part") then
			v.Transparency = 0
			v.CanCollide = true
		end
	end
	script.parent:Destroy()
end)

3 comments

0

The LocalPlayer pointer of the Player's Service, refers to the Client's affiliated Player Instance. You're are using a Server-sided Script—it is run on the Server—which means the attributes familiar to a local environment will become foreign; the Server does not have a Player Object of its own, which is why 'LocalPlayer' returns nil.

0

Fortunately, with .MouseClick RBXScriptSignal of ClickDetector passes the Player that clicked the BasePart as an argument to your callback, you can use that.

0

Kk thanks.