LocalPlayer ScreenUI Error "attemp to index nil with leaderstats"

Backstory: I’m trying to make a currency system that checks if you enough funds to select the betting option

1 Like

Can you send us the script so we can see whats wrong?

1 Like
--// Betting Section

local bet = 0
local Error = script.Parent.Parent.ErrorText
local CurrentText = script.Parent.Parent.CurrentBet 

if game.Players.LocalPlayer.leaderstats.Cash == nil then
		repeat
		until game.Players.LocalPlayer.leaderstats.Cash ~= nil
end

local A = script.Parent.Parent.bet10
function B1()
	if game.Players.LocalPlayer.leaderstats.Cash >= 10 then
	bet = 10
	print('Current Bet: '..bet)
		CurrentText.Text = ('Current Bet: '..bet)
	else
		Error.Visible = true
		wait(3)
		Error.Visible = false
	end
end
A.MouseButton1Click:Connect(B1) 

local B = script.Parent.Parent.bet25
function B2()
	if game.Players.LocalPlayer.leaderstats.Cash >= 25 then
	bet = 25
	print('Current Bet: '..bet)
	CurrentText.Text = ('Current Bet: '..bet)
	else
		Error.Visible = true
		wait(3)
		Error.Visible = false
	end
end
B.MouseButton1Click:Connect(B2) 

local C = script.Parent.Parent.bet50
function B3()
	if game.Players.LocalPlayer.leaderstats.Cash >= 50 then
	bet = 50
	print('Current Bet: '..bet)
	CurrentText.Text = ('Current Bet: '..bet)
	else
	Error.Visible = true
	wait(3)
	Error.Visible = false
end
end
C.MouseButton1Click:Connect(B3) 

local D = script.Parent.Parent.bet75
function B4()
	if game.Players.LocalPlayer.leaderstats.Cash >= 75 then
	bet = 75
	print('Current Bet: '..bet)
	CurrentText.Text = ('Current Bet: '..bet)
	Error.Visible = true
	wait(3)
	Error.Visible = false
end
end
D.MouseButton1Click:Connect(B4) 

local E = script.Parent.Parent.bet100
function B5()
	if game.Players.LocalPlayer.leaderstats.Cash >= 100 then
	bet = 100
	print('Current Bet: '..bet)
	CurrentText.Text = ('Current Bet: '..bet)
	Error.Visible = true
	wait(3)
	Error.Visible = false
end
end
E.MouseButton1Click:Connect(B5) 

--// Spawn Ball Section

function leftClick()
local SS = game:GetService("ServerStorage")

	local BallList = SS:WaitForChild("PlinkoBalls")

	local Balls = BallList:GetChildren()
	local items = BallList:GetChildren()
	local randomBall = items[math.random(1, #items)]
	
	
	print("Plink Ball")
	local ballCopy = randomBall:Clone()
	for i = 1, #Balls do
		ballCopy.Parent = game.Workspace.Plinko.Balls
		ballCopy.Name = "PlinkoBall"
		ballCopy.Value.Value = bet
	end
	end

script.Parent.MouseButton1Click:Connect(leftClick)
1 Like

image
The idea behind it is that if you don’t have equal to or the bet amount then it will say
“insufficient funds” that way people can’t bet money they don’t have
image
Only issue is that it doesn’t allow me to get the path for leaderboard so the script errors

1 Like

Is this a script or a local script?

1 Like

This is inside a normal script not local
image

1 Like

Then, game.Players.LocalPlayer will return nil if the script is a normal script.

1 Like

So what should I do, change it to a local script?

1 Like

Move this code over to a local script and make use of RemoteEvents in order to pass the data to the server.

2 Likes


With it as a local script it still errors but in a different way

1 Like

image

change leaderstats.Cash to leaderstats.Cash.Value

1 Like


Ah yes thank you for catching my mistake I appreciate that, what were you saying about remote events?

1 Like

Remote Events are used to send data from the client (player) to the server. Anything client sided can only be viewed by the client whereas changed made on the server are global.

1 Like
--// Spawn Ball Section

function SpawnBall()
	local SS = game:GetService("ServerStorage")

	local BallList = SS:WaitForChild("PlinkoBalls")

	local Balls = BallList:GetChildren()
	local items = BallList:GetChildren()
	local randomBall = items[math.random(1, #items)]


	print("Plink Ball Purchased")
	local ballCopy = randomBall:Clone()
	for i = 1, #Balls do
		ballCopy.Parent = game.Workspace.Plinko.Balls
		ballCopy.Name = "PlinkoBall"
		ballCopy.Value.Value = bet
	end
end

script.Parent.MouseButton1Click:Connect(SpawnBall)

I’ve seemed to run into a new issue it doesn’t let me spawn balls

1 Like

ServerStorage cannot be accessed from the client.

1 Like

So a local script won’t be able to get the ServerStorage? What if I changed the location to replicated storage?

1 Like

That should work perfectly without any issues.

1 Like

It’s like every fix makes a new issue, now the detectors won’t work when the ball hits

script.Parent.Touched:Connect(function(h)
	if h:IsA("Part") and h.Name == "PlinkoBall" then
		print('1.0x Triggered')
		wait(0.2)
		local outcome = h.Value.Value * 1.0
		print(outcome)
		h:Destroy()
	end
end)

image

1 Like

Any ideas on what can be causing it to mess up? ^

1 Like

is that a client sided script or a server sided script?

1 Like