game.Players.PlayerAdded:Connect(function(player)
script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
local players = game:GetService("Players")
local clone = game.Workspace.Sparkles:Clone()
clone.Parent = game.Workspace
clone.CanCollide = false
clone.Position = script.Parent.Position
script.Parent:Destroy()
wait(1)
clone:Destroy()
local player = game.players.LocalPlayer
players.LocalPlayer.leaderstats.gold.value = 10
end
end)
end)
Here, I'm trying to make a script where if you touch a chest it will give you gold[my stat name] but for some reason, it wont run properly. it wont give me the amount of gold i told it to
From my understanding, you have a function that fires whenever a player joins the game. And then when the part is touched, the part then fires the code you have. I would remove the game.Players.PlayerAdded:Connect(function(player)
function as when it get's hit it will fire for every single player who has joined.
But why am I not gaining any gold?
If you are using a server sided script you can't just call game.Players.LocalPlayer
as that is client sided. LocalPlayer is an object that is in client sided scripts that tell the client sided script(s) what player they are executing on.
How do I fix this? First, you already have a statement to decide whether the hit part is a character. So, you can do,
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
Player.leaderstats.gold.Value += 10
I've fixed a few things in your scripts and removed some things as well. First, I added a += 10
to the gold adding script as if you don't it will just set the gold value to 10 and never add any more.
LocalPlayer is not available to be used on server sided scripts and should only be used on Client sided.
(For future references, in your first initial code you have code that sets the variable of player
but under that line you call players
. (however, that wouldn't still work. Just trying to provide information about code that's syntax was incorrect.))