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

Attempt to index nil with 'leaderstats'??

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

✓ Accepted answer

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.

10 comments

0

Gonna try it now, can't believe I overlooked that but the while true do loop can work with the function, right?

0

It worked.

0

glad i could be of help ^^

0

Yh man, but for some reason the star doesn't destroy, I keep having that problem. Any reason why??

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.

0

Okay I will handle that now.

0

Posted it.

0

I posted it as an answer, just scroll down ;)

0

in the first script i would delete that wait(1) and i'd put if(player.leaderstats.Level.Value >= script.Levels.Value) maybe?

0

That would exhaust the script.

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.

1 comment

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.

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