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.
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)
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.