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

Workspace.Part.SurfaceGui.Frame.Script:19: attempt to index global 'Player' (a nil value)?

So I have a part in the workspace and a surface gui and all of that. I originally tried a LocalScript but it didnt work, so I changed it to a server script. Everything worked until I added these things

1:

Player = game:GetService("Players").LocalPlayer

2:

Upgrade.MouseButton1Click:connect(function()
	if(Player.leaderstats.Cash.Value >= Variables.UpgradeCash.Value) then
		Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - Variables.UpgradeCash.Value
	end
end)

The script before that was just

Upgrade = script.Parent.Upgrade
Level = script.Parent.Levels
DC = script.Parent.DC
UpgradeFrame = script.Parent.Parent.Upgrade
LevelFrame = script.Parent.Parent.Levels
UpgradeCash = script.Parent.Parent.Upgrade.CashUpgrade
Variables = script.Parent.Parent.Parent.Variables

Upgrade.MouseButton1Click:connect(function()
	UpgradeFrame.Visible = true
	LevelFrame.Visible = false
end)
Level.MouseButton1Click:connect(function()
	UpgradeFrame.Visible = false
	LevelFrame.Visible = true
end)

When I added those two things I got this error

15:49:12.326 - Workspace.Part.SurfaceGui.Frame.Script:19: attempt to index global 'Player' (a nil value)
15:49:12.330 - Stack Begin
15:49:12.332 - Script 'Workspace.Part.SurfaceGui.Frame.Script', Line 19
15:49:12.333 - Stack End

Heres my full script:

Upgrade = script.Parent.Upgrade
Level = script.Parent.Levels
DC = script.Parent.DC
UpgradeFrame = script.Parent.Parent.Upgrade
LevelFrame = script.Parent.Parent.Levels
UpgradeCash = script.Parent.Parent.Upgrade.CashUpgrade
Variables = script.Parent.Parent.Parent.Variables
Player = game:GetService("Players").LocalPlayer

Upgrade.MouseButton1Click:connect(function()
	UpgradeFrame.Visible = true
	LevelFrame.Visible = false
end)
Level.MouseButton1Click:connect(function()
	UpgradeFrame.Visible = false
	LevelFrame.Visible = true
end)
Upgrade.MouseButton1Click:connect(function()
	if(Player.leaderstats.Cash.Value >= Variables.UpgradeCash.Value) then
		Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - Variables.UpgradeCash.Value
	end
end)

4 comments

0

All guis need to run on the client. So you have to use a local script. Put the local script in StarterGui and edit your variables

0

The above is true, but obviously you're using a SurfaceGui. Local Scripts Don't work in workspace and Regular scripts can't access Local Player.

0

Im sorry if you read wrong, but Im using a surface gui... On a brick...

0

Oops lol. That's what I meant. I'll just edit that :3

1 answer

I've decided to answer because you were being nice in the comments and I'm super bored,

So because we have the option, we're going to be using Local Scripts.

So let's start of by making a Local Script and putting it in StarterPack,

-- Local Script in StarterPack
A better place to put the Local Script would be StarterPlayerScripts.

Now you only moved the script, not the model. This means we have to re-define all the variable. This part your mostly going to have to do yourself because I don't know where anything is.

-- Local Script in StarterPack
local Player = game:GetService("Players").LocalPlayer
-- Your going to have to define these variables corectly,
local Model = workspace.Model
local Upgrade = Model.Upgrade
local Level = Model.Levels
local DC = Model.DC
local UpgradeFrame = Model.Upgrade
local LevelFrame = Model.Levels
local UpgradeCash = Model.Upgrade.CashUpgrade
local Variables = Model.Variables

Now all we have to do is add your code,

-- Local Script in StarterPack
local Player = game:GetService("Players").LocalPlayer
-- Your going to have to define these variables corectly,
local Model = workspace.Model
local Upgrade = Model.Upgrade
local Level = Model.Levels
local DC = Model.DC
local UpgradeFrame = Model.Upgrade
local LevelFrame = Model.Levels
local UpgradeCash = Model.Upgrade.CashUpgrade
local Variables = Model.Variables

Upgrade.MouseButton1Click:connect(function()
    UpgradeFrame.Visible = true
    LevelFrame.Visible = false
end)
Level.MouseButton1Click:connect(function()
    UpgradeFrame.Visible = false
    LevelFrame.Visible = true
end)
Upgrade.MouseButton1Click:connect(function()
    if(Player.leaderstats.Cash.Value >= Variables.UpgradeCash.Value) then
        Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - Variables.UpgradeCash.Value
    end
end)

Side note,

When FE is on, you can't change leaderstats values globally.

Good Luck!

If I did help, please don't forget to accept my answer.

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