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

Leaderstat value doesn't change?

Asked by 4 years ago
Edited 4 years ago
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.

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. NotedAPI 810 — 4y
0
You also forgot to say Stage.Value NotedAPI 810 — 4y
0
oh JustSxript 36 — 4y

2 answers

Log in to vote
1
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)
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 raid6n 2196 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

0
U got improper terms (you can't "call" localplayer or an event), and tons of mistakes make sure to preview your posts. programmerHere 371 — 4y
0
^^ Thanks. Sebgamingkid 147 — 4y
0
I'm sorry but I do not understand. Please explain this just fully, without steps. JustSxript 36 — 4y
0
^^ I did my best, if my answer is not helpful then feel free to not mark it as the answer. Sebgamingkid 147 — 4y
0
I'll come back at this tomorrow. JustSxript 36 — 4y

Answer this question