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'
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
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)
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)