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

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

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:

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")

remote.OnServerEvent:connect(function(player)
	local player = game.Players.LocalPlayer
local gui = game.Workspace.PlusOne
local Clicked = false

gui.Parent = nil 

	wait(1)
	local Clicked = true
    gui.Parent = player.PlayerGui -- The error said it was comeing from this line!
wait(.8)
game.Players.LocalPlayer.leaderstats.Coins.Value = game.Players.LocalPlayer.leaderstats.Coins.Value + 1
gui.Parent = nil
Clicked = false
while true do wait()
	
	
	end

end)


2 answers

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:

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")

remote.OnServerEvent:connect(function(player)
local gui = game.Workspace.PlusOne
local Clicked = false

gui.Parent = nil 

wait(1)
local Clicked = true
gui.Parent = player.PlayerGui
wait(.8)
game.Players.LocalPlayer.leaderstats.Coins.Value = game.Players.LocalPlayer.leaderstats.Coins.Value + 1
gui.Parent = nil
Clicked = false
while true do wait()

end
end)

2 comments

0

Do not encourage poorly spaced and indented code please.

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.

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

local gui = game.StarterGui.PlusOne
     script.Parent.MouseButton1Click:Connect(function(plr)
     local leaderstats = plr:WaitForChild("leaderstats") if leaderstats then
     local cash = leaderstats:FindFirstChild("Cash") if cash then
     gui.Enabled = true
     cash.Value = cash.Value + 1 
end
end
    end)

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

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