Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 3 years ago
Edited 3 years ago

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?

0
Is this a server script? deeskaalstickman649 475 — 3y

3 answers

Log in to vote
1
Answered by
pwx 1581 Moderation Voter
3 years ago

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
0
you are right, but this have no playability if you gotta set the player himself because it's only for 1 person Xapelize 2658 — 3y
Ad
Log in to vote
1
Answered by 3 years ago
Edited 3 years ago
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)
0
this is a very ??? script, bad practise but i like it Xapelize 2658 — 3y
0
wait, actually it works Xapelize 2658 — 3y
Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

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

Answer this question