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