Script not working

My script is not working it is supposed to check if a player has enough money to use a wheel spin

The script

local list = {
	
	"Birthday RPG",
	"Birthday Z6",
	"Heal Gun",
	"Pink Double Saber"
}

local plr = game.Players.LocalPlayer

local Enabled = true

script.Parent.MouseButton1Click:Connect(function()
	if plr.leaderstats.Money.Value <= 1 then
		if Enabled == false then return end
	Enabled = false
	
	script.Parent.Parent.Done.Start.Disabled = true
	script.Parent.Parent:WaitForChild("Text").Text = "Wheel Spin"
	local Time = 0.025
	
	for i = 1, 75 do
		script.Parent.Parent.Result.Text = list[math.random(1, #list)]
		
		if i == 50 then Time = 0.05 end
		if i == 55 then Time = 0.1 end
		if i == 60 then Time = 0.2 end
		if i == 65 then Time = 0.4 end
		if i == 70 then Time = 0.8 end
		if i == 75 then
			script.Parent.Parent:WaitForChild("Text").Text = "You have received a..."
			script.Parent.Parent.Done.Start.Disabled = false
			Enabled = true
		end
		wait(Time)
	end
	
	end
end)

the output

Players.JadtrugamingYT1.PlayerGui.ScreenGui.Background.Spin.Main:14: attempt to index nil with 'leaderstats'

Maybe you spelled it wrong here

It’s unclear what is causing the problem, maybe try setting the variable plr to the player every time the function runs to be sure that the variable is the LocalPlayer? Let me know if this helps, it will probably help me in the future, also :stuck_out_tongue:

1 Like
  1. Is the script a localscript? If not, you need a LocalScript for this.

  2. Make sure leaderstats actually exists. (You can check in the explorer while playing the game in studio)

This would make it very easily exploitable if it was in a local script since the client can change the leaderstat value to whatever they want.

1 Like

It has to be in a localscript though, no way around it. (Because of GUI)

They can use RemoteEvents to do any changing, though

game.Players.LocalPlayer can only be used in a LocalScript.

But don’t do this in a local script since it’s very insecure the way you’re handling it right now.

True, though that fix with making it a local script will introduce many other problems.

Try changing the script to a server side one and then paste this code:

local list = {
	
	"Birthday RPG",
	"Birthday Z6",
	"Heal Gun",
	"Pink Double Saber"
}


local Enabled = true

script.Parent.MouseButton1Click:Connect(function(plr)
	if plr.leaderstats.Money.Value <= 1 then
		if Enabled == false then return end
	Enabled = false
	
	script.Parent.Parent.Done.Start.Disabled = true
	script.Parent.Parent:WaitForChild("Text").Text = "Wheel Spin"
	local Time = 0.025
	
	for i = 1, 75 do
		script.Parent.Parent.Result.Text = list[math.random(1, #list)]
		
		if i == 50 then Time = 0.05 end
		if i == 55 then Time = 0.1 end
		if i == 60 then Time = 0.2 end
		if i == 65 then Time = 0.4 end
		if i == 70 then Time = 0.8 end
		if i == 75 then
			script.Parent.Parent:WaitForChild("Text").Text = "You have received a..."
			script.Parent.Parent.Done.Start.Disabled = false
			Enabled = true
		end
		wait(Time)
	end
	
	end
end)

wont work because server side scripts cant change gui, but remoteevents could be used for communication.

Do you meant to put less than like shown here:

or do you mean to put greater than?

What do you mean? I use a lot of server scripts when changing UI properties.

That’s not the problem. The script doesn’t finds the leaderstats.

1 Like

I think I was kinda mistaken, server side scripts should be able to change it, but wont be able to use functions like

button.Activated:Connect(function()

end)

That is useless. As far as I can see from the script, it just changes the text property. About the click function, that should work too.

No, it has a MouseButton1Click Event.

script.Parent.MouseButton1Click:Connect(function(plr)

You can fire a remote event when the gui button is clicked. From there you can do everything you need to do with the gui. On the local script you can also fire the remote event to pass the player argument for the player variable. I think the reason it cannot find leaderstats is because you cannot do game.Players.LocalPlayer in a server script.

Code Example:

--ServerScript--
local list = {

"Birthday RPG",
"Birthday Z6",
"Heal Gun",
"Pink Double Saber"
}

local repStore = game:GetService("ReplicatedStorage")
local remoteEvent = repStore:WaitForChild("RemoteEvent")

local player
remoteEvent.OnServerEvent(Connect(function(player)
    plr = game.Players.LocalPlayer
end)

local Enabled = true

script.Parent.MouseButton1Click:Connect(function()
if plr.leaderstats.Money.Value <= 1 then
            remoteEvent:FireClient(plr)
	if Enabled == false then return end
Enabled = false

end
end)
--local script--
local remoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
remoteEvent:FireServer()

remoteEvent.OnClientEvent:Connect(function(player)
    script.Parent.Parent.Done.Start.Disabled = true
script.Parent.Parent:WaitForChild("Text").Text = "Wheel Spin"
local Time = 0.025

for i = 1, 75 do
	script.Parent.Parent.Result.Text = list[math.random(1, #list)]
	
	if i == 50 then Time = 0.05 end
	if i == 55 then Time = 0.1 end
	if i == 60 then Time = 0.2 end
	if i == 65 then Time = 0.4 end
	if i == 70 then Time = 0.8 end
	if i == 75 then
		script.Parent.Parent:WaitForChild("Text").Text = "You have received a..."
		script.Parent.Parent.Done.Start.Disabled = false
		Enabled = true
	end
	wait(Time)
end
end)

Try this. If there are any errors tell me.