So i want to make a game, it has saving progress but i dont know how do i make adding gold beacuse when i put this script to brick:
function addGOLD() game.Players.LocalPlayer.leaderstats.Gold.Value += 25 end script.Parent.Touched:Connect(addGOLD)
it shows:
09:34:01.855 Workspace.Part.Script:2: attempt to index nil with 'leaderstats' - Server - Script:2 09:34:01.855 Stack Begin - Studio 09:34:01.855 Script 'Workspace.Part.Script', Line 2 - function addGOLD - Studio - Script:2 09:34:01.855 Stack End - Studio
but when i type this in command bar:
game.Players.LocalPlayer.leaderstats.Gold.Value += 25
it works, so wheres the problem?
You can't get the LocalPlayer through the server.
Just define your player through Player Service.
local Players = game:GetService('Players') local Player = Players['USERNAME'] Player:WaitForChild('leaderstats').Gold.Value += 25
local Players,debounce=game:GetService"Players",nil local function addGOLD(part) if debounce then return end local player=Players:GetPlayerFromCharacter(part.Parent) if player then player.leaderstats.Gold.Value+=25 debounce=true wait(1) debounce=false end;end;script.Parent.Touched:Connect(addGOLD)
Because console is client because you are using it on client, but you cannot use LocalPlayer in normal Scripts, here's one of the way to get player parameter and it's the easiest understandable way:
game:GetService("Players").PlayerAdded:Connect(function(player) -- player is the LocalPlayer that you are looking for! end
Heres the script:
game:GetService("Players").PlayerAdded:Connect(function(player) -- get player parameter, also the localplayer thing game.Workspace.PartName.Touched:Connect(function(hit) -- get the hit (WHO HITTED) if hit.Parent.Name == player.Name then -- if the hit player is the player parameter defined above player:WaitForChild("leaderstats").Gold.Value += 25 -- give player some sweet gold :tasty: end end end