Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Gold adder dont work but in command bar works? help me!

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?

1 comment

0

Is this a server script?

3 answers

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

1 comment

0

you are right, but this have no playability if you gotta set the player himself because it's only for 1 person

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)

2 comments

0

this is a very ??? script, bad practise but i like it

0

wait, actually it works

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

This discussion is preserved as it was. New answers, comments, and votes are closed.