Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to index nil with 'leaderstats'??

Asked by 3 years ago

I need help with this script. I am kinda confused about using functions and loops at the same time, don't know if it will work. Here is the script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("Arrow")

local player = game.Players.LocalPlayer
local ServerStorage = game:GetService("ServerStorage")
local star = ServerStorage:WaitForChild("Star")
local cloneStar = star:Clone()
cloneStar.Parent = game.Workspace

local function onSpawnStar(cloneStar)
while true do
wait(.1)
local level = player.leaderstats.Level.Value
if game.Workspace.Checkpoints:FindFirstChild(level + 1) then
    local ls = game.Workspace.Checkpoints:FindFirstChild(level + 1)
        cloneStar:MoveTo(ls.Position + Vector3.new(0,10,0))
    end
    wait(1)
    if(player.leaderstats.Level.Value == script.Levels.Value)then
        cloneStar:Destroy()
end
    if(player.Character.Humanoid.Health == 0)then
        cloneStar:Destroy()
        end
    end
end

remoteEvent.OnServerEvent:Connect(onSpawnStar)

2 answers

Log in to vote
1
Answered by
Echtic 128
3 years ago

Seeing as you are using the OnServerEvent event i'm guesing this is a regular and not a local script, which means you can't be defining the player using:

 game.Players.LocalPlayer

which is 99% why you are getting the "Attempt to index nil with 'leaderstats'" error. The player can be gotten from the OnServerEvent as an argument, which i suggest doing. Here's a roblox wiki article about that.

0
Gonna try it now, can't believe I overlooked that but the while true do loop can work with the function, right? nathanielstanle 20 — 3y
0
It worked. nathanielstanle 20 — 3y
0
glad i could be of help ^^ Echtic 128 — 3y
0
Yh man, but for some reason the star doesn't destroy, I keep having that problem. Any reason why?? nathanielstanle 20 — 3y
View all comments (6 more)
0
I'd need more information on what's that "Levels" value inside the script and how does it change in order to try to figure out why it doesn't work as intended. Echtic 128 — 3y
0
Okay I will handle that now. nathanielstanle 20 — 3y
0
Posted it. nathanielstanle 20 — 3y
0
I posted it as an answer, just scroll down ;) nathanielstanle 20 — 3y
0
in the first script i would delete that wait(1) and i'd put if(player.leaderstats.Level.Value >= script.Levels.Value) maybe? Echtic 128 — 3y
0
That would exhaust the script. nathanielstanle 20 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The error you're getting is because you're trying to use LocalPlayer on the server, it won't work because the server isn't associated with any of the clients so it'll only return nil (hence the error). LocalPlayer can only be obtained through LocalScripts as LocalScripts handle things on the client.

When firing RemoteEvents to the server, it automatically passes the player that fired it, so the first parameter on OnServerEvent is always going to be the player. At the moment with your script, your cloneStar parameter is actually the player that fired the RemoteEvent.

Remove your player variable and put "player" as your first parameter in your onSpawnStar function.

0
Thanks man, figured it out but I have a problem. For some reason the Star does not destroy when I reach the last stage, I don't know why it keeps happening. nathanielstanle 20 — 3y

Answer this question