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

attempt to index local 'player' (a nil value)?

Asked by 6 years ago
Edited 6 years ago

Basically, I need a script for a tool that opens a GUI locally then ads coins to the leader stats but there was an error that had an issue when line 13. Also, this is the script for the remote. Please help me thanks:

01local repStorage = game:GetService("ReplicatedStorage")
02local remote = repStorage:WaitForChild("Remote")
03 
04remote.OnServerEvent:connect(function(player)
05    local player = game.Players.LocalPlayer
06local gui = game.Workspace.PlusOne
07local Clicked = false
08 
09gui.Parent = nil
10 
11    wait(1)
12    local Clicked = true
13    gui.Parent = player.PlayerGui -- The error said it was comeing from this line!
14wait(.8)
15game.Players.LocalPlayer.leaderstats.Coins.Value = game.Players.LocalPlayer.leaderstats.Coins.Value + 1
View all 23 lines...

2 answers

Log in to vote
0
Answered by 6 years ago

It is because you tried to redefine 'player' as if it were in a local script, you don't need to redefine player once you get it from an OnServerEvent, here's a refixed result:

01local repStorage = game:GetService("ReplicatedStorage")
02local remote = repStorage:WaitForChild("Remote")
03 
04remote.OnServerEvent:connect(function(player)
05local gui = game.Workspace.PlusOne
06local Clicked = false
07 
08gui.Parent = nil
09 
10wait(1)
11local Clicked = true
12gui.Parent = player.PlayerGui
13wait(.8)
14game.Players.LocalPlayer.leaderstats.Coins.Value = game.Players.LocalPlayer.leaderstats.Coins.Value + 1
15gui.Parent = nil
16Clicked = false
17while true do wait()
18 
19end
20end)
0
Do not encourage poorly spaced and indented code please. User#25115 0 — 6y
0
Oh, well I didn't like it either, but I continued the same format for his sakes, there's probably a reasoning behind it we all do not know. Cvieyra2test 176 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This should be in a Local Script in the tool. Also move ur gui into the StarterGui and set it to disabled

1local gui = game.StarterGui.PlusOne
2     script.Parent.MouseButton1Click:Connect(function(plr)
3     local leaderstats = plr:WaitForChild("leaderstats") if leaderstats then
4     local cash = leaderstats:FindFirstChild("Cash") if cash then
5     gui.Enabled = true
6     cash.Value = cash.Value + 1
7end
8end
9    end)

There. Way shorter and it should work. If it doesnt work then comment and i'll see for any problems.

Answer this question