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

Why doesn't this server script recognize the character?

I really don't know why this doesn't work. The script doesn't wait for the player, instead it calls it a nil value. What do i do?

plr = game.Players:WaitForChild("LocalPlayer")
char = plr:WaitForChild("Character")
hum = char:WaitForChild("Humanoid")

hum.Died:Connect(function()
	wait(5)
	game:GetService("ReplicatedStorage").LC:FireServer()
end)

1 comment

0

Ur saying this is a serverscript? But u can't? Because it need's to be a localscript

1 answer

✓ Accepted answer

###This is because properties are not children of objects. The value of LocalPlayer is in fact a child of the Players service, but the property itself is not. Your script is literally waiting for a user named "LocalPlayer" to join the game.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait() -- if character returns nil, we will wait instead 
local hum = char:WaitForChild("Humanoid")

hum.Died:Connect(function()
    wait(5)
    game:GetService("ReplicatedStorage").LC:FireServer()
end)

###It is also recommended to always use local variables, as globales are bad practice because they clutter the global environment, and if used improperly make your code messy.

###EDIT: I just read you were using a Script to access LocalPlayer. Sadly, this will not work.

#Ancestry

###The top ancestor (excluding the DataModel and the Players service) of a LocalScript is, well, the Player object/LocalPlayer unless it was placed in ReplicatedFirst. Objects in ReplicatedFirst are the first thing to be replicated to the client, and not the server. And LocalScripts run on behalf of the client. Scripts run on behalf of the server.

#Why LocalPlayer is nil on the server

###Think of it as your school classroom. Your desk is your desk, just like how your LocalScript is your LocalScript, it works for you. But your classroom is your classroom, and in it are your classmates as well as your teacher. Think of the classroom as the server. It has everyone in it, and cannot keep track of who in the heck LocalPlayer is! For this reason, LocalPlayer is nil on the server. The server isn't meant to do work for one specific player, but for all. Your game could have multiple players in it, and when you try accessing LocalPlayer, it can't just randomly pick a player. Code cannot read your mind. This is why it must be all typed out. So instead of doing this:

make a part inside of the workspace with color red

###You must do this:

local part = Instance.new("Part")
part.BrickColor = BrickColor.new("Really red")
part.Parent = workspace 

1 comment

0

the script only respawns once now? Whats happening?

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