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

Error "attempt to index field 'LocalPlayer' (a nil value)???

My game works perfectly in studio but it does not when published and played as a game. Can someone help please!

local ui = game.Players.LocalPlayer.PlayerGui
local c = 50
local v = 0
local sgui = Instance.new("ScreenGui")
sgui.Parent = ui
local textLabel = Instance.new("TextLabel")
textLabel.Parent = sgui
textLabel.Font = 12
textLabel.BackgroundColor3 = Color3.new(0,0.4,1)
textLabel.TextColor3 = Color3.new(0,0,0)
textLabel.BorderColor3 = Color3.new(0,0,0)
textLabel.BorderSizePixel = 4
textLabel.Text = "($"..c..") Amateur Swordsman(+1) Owned: 0"
textLabel.TextSize = 30
textLabel.Size = UDim2.new(.45,0,0,80)
textLabel.Position = UDim2.new(.275,0,0,100)
textLabel.Visible = false

local buyButton = Instance.new("TextButton")
buyButton.Parent = sgui
buyButton.Font = 12
buyButton.BackgroundColor3 = Color3.new(0,0,0)
buyButton.TextColor3 = Color3.new(1,1,1)
buyButton.BorderColor3 = Color3.new(1,1,1)
buyButton.BorderSizePixel = 4
buyButton.Text = "Purchase"
buyButton.TextSize = 30
buyButton.Size = UDim2.new(.1,0,0,35)
buyButton.Position = UDim2.new(.725,20,0,100)
buyButton.Visible = false

local Amount = Instance.new("TextBox")
Amount.Parent = sgui
Amount.Font = 12
Amount.BackgroundColor3 = Color3.new(0,0,0)
Amount.TextColor3 = Color3.new(1,1,1)
Amount.BorderColor3 = Color3.new(1,1,1)
Amount.BorderSizePixel = 4
Amount.Text = 1
Amount.TextSize = 30
Amount.Size = UDim2.new(.05,0,0,35)
Amount.Position = UDim2.new(.75,20,0,145)
Amount.Visible = false

function round(c)
	return math.floor(c+.5)
end

buyButton.MouseButton1Down:connect(function()
	local player = game.Players.LocalPlayer
	for _, player in pairs(game.Players:GetPlayers()) do
		if game.Players.LocalPlayer.leaderstats.Cash.Value >= c
			then game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value - c
	game.Workspace.Upgrade.UpgradeValue.Value = game.Workspace.Upgrade.UpgradeValue.Value + Amount.Text
	buyButton.Text = "Purchased"
	c = 50
	wait(.01)
	v = v + Amount.Text
	c = round(Amount.Text * c * 1.025^(v + Amount.Text) - 1)
	textLabel.Text = "($"..c..") Amateur Swordsman(Attack+1) Owned: ".. v
	wait(1)
	buyButton.Text = "Purchase"
		else
			buyButton.TextSize = 17
			buyButton.Text = "Not enought cash!"
			wait(.5)
			buyButton.TextSize = 30
			buyButton.Text = "Purchase"
		end
		end
end)

Amount.FocusLost:connect(function()
	c = 50
	wait()
	c = round(Amount.Text * c * 1.025^(v + Amount.Text) - 1)
		textLabel.Text = "($"..c..") Amateur Swordsman(Attack+1) Owned: ".. v
end)

repeat wait(0.01)
if game.Workspace.Shop.Visibility.Value == 1
	then
	buyButton.Visible = true
	textLabel.Visible = true
	Amount.Visible = true
elseif game.Workspace.Shop.Visibility.Value == 0
	then
	Amount.Visible = false
	buyButton.Visible = false
	textLabel.Visible = false
end
until game.Workspace.Shop.Visibility.Value == 2

3 comments

0

Can you show the source script that the error is coming from? Otherwise we can't do anything to help.

0

It's coming from about every script, and there are about 10 total. I'll post an example.

0

are you calling local player inside a local script?

1 answer

A You didn't put the LocalPlayer script in a Local Script, or

B LocalPlayer hasn't loaded in before the script loads in. This will result in an error with Lua thinking that LocalPlayer never existed. You can fix it using WaitForChild().

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