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

Leaderstat value doesn't change?

local Brick = script.Parent

local function PlayerTouched(Part)
game.Players.LocalPlayer.leaderstats.Stage = game.Players.LocalPlayer.leaderstats.Stage + 1
game.StarterGui.Cash.Cash.Text = ("Stage: 1")
script.Parent:Destroy()
	end
Brick.Touched:connect(PlayerTouched)

This is supposed to change the text of an gui and it should also change the leaderstat called "Stage" Error: Workspace.Statchanger.Code:7: attempt to index nil with 'leaderstats'

Please help me.

3 comments

0

Few things wrong with this script. You aren't suppose to do game.StarterGui, it Player.PlayerGui, you also can't call LocalPlayer from a normal script.

0

You also forgot to say Stage.Value

0

oh

2 answers

Your script:


local Brick = script.Parent

local function PlayerTouched(Part) -- 
game.Players.LocalPlayer.leaderstats.Stage = game.Players.LocalPlayer.leaderstats.Stage + 1 -- good job, but I do recommend using waitforchild and do stage.value
game.StarterGui.Cash.Cash.Text = ("Stage: 1") 
script.Parent:Destroy()
    end
Brick.Touched:connect(PlayerTouched)

Edited Script (Local Script)


local function PlayerTouched(Part)
local plr = game.Players.LocalPlayer
local Stage = plr:WaitForChild ("Stage") 
Stage.Value = Stage.Value + 1
game.StarterGui.Cash.Cash.Text = ("Stage: 1") 

script.Parent:Destroy()
    end
Brick.Touched:connect(PlayerTouched)

Edited Script (Server Script)


local function PlayerTouched(Part)

local plr = game.Players:GetPlayerFromCharacter(Part.Parent)
local Stage = plr:WaitForChild ("Stage") 
Stage.Value = Stage.Value + 1
plr.PlayerGui.Cash.Cash.Text = ("Stage: 1") 

script.Parent:Destroy()
    end
Brick.Touched:connect(PlayerTouched)

1 comment

0

If you would like me to explain why the other scripts would work in better detail, I will edit if needed. The explanation is in your starting script

I see the issue. First thing I notice is that this is a local script. Meaning that you cannot call the 'Touched' event from it. You can get a player from a touched brick easily, you first check that touch part is a real player part.

local function PlayerTouched(prt)
	local humanoid = prt.Parent:FindFirstChild("Humanoid")
	if humanoid then
		-- Code
	end

The function ":FindFirstChild()" will take it's argument, and if it finds the object inside of instance before it, then it will return true. In this case, a player touches (let's say, their arm) and then, it goes: prt.Parent (which would be the player model), then it goes :FindFirstChild("Humanoid"), so it looks for "Humanoid" inside the player model, it's a player model so it finds it and returns true! This means our "if" statement becomes true and runs the following code block in it.

Then you get their player.

local plr = game.Players:GetPlayerFromCharacter(prt.Parent)

"GetPlayerFromCharacter" basically takes the character model as an argument, to find the player instance. In this case, the player model is prt.Parent so we give it prt.Parent and it returns the player instance.

Then we edit the values as such!

local stage = plr.PlayerGui.leaderstats.Stage
local stageVal = stage.Value

Run this all from a normal script inside the brick.

EXTRA note: Local scripts are the only scripts able to call "Local Player", normal scripts can not as they are run from a server and not a client.

5 comments

0

U got improper terms (you can't "call" localplayer or an event), and tons of mistakes make sure to preview your posts.

0

^^ Thanks.

0

I'm sorry but I do not understand. Please explain this just fully, without steps.

0

^^ I did my best, if my answer is not helpful then feel free to not mark it as the answer.

0

I'll come back at this tomorrow.

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