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

When I try to get something from player like character it says attempt to index nil with Character.?

I tried putting a script into ServerScriptService to determine when a player dies, but it dowsnt work! ~~~~~~~~~~~~~~~~~

local plrs = game:GetService("Players")

local plr = plrs.LocalPlayer

local char = plr.Character or plr.Character:Wait()

char.Humanoid.Died:Connect(function() game:GetService("Chat"):Chat(workspace.Radio, plr.Name "Has Died", Enum.ChatColor.Red) end)


3 answers

✓ Accepted answer

You can't use LocalPlayer inside a Server Script, most of people do that. It's only used on LocalScript. To get the player, use the playerAdded event.

game.Players.PlayerAdded:Connect(function(player)
    -- code here
end)

Also, to get the character, use the characterAdded event.

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        -- code here
    end)
end)

1 comment

0

Thank you so much! Highly appreciated.

Your error is on the line:

local char = plr.Character or plr.Character:Wait()

It should be:

local char = plr.Character or plr.CharacterAdded:Wait()

2 comments

0

I tried that but it did not work still /:

0

Yeah because you can't use that on a server script, only for LocalScripts

Try this:

local plrs = game.Players:GetChildren()
for i = 1,#plrs do
plrs[i].Humanoid.Died:Connect(function()
local char = plr.Character
game:GetService("Chat"):Chat(workspace.Radio, plr.Name "Has Died", Enum.ChatColor.Red) end)
end

Give me a message if this doesn't work

4 comments

0

'plrs[i].Humanoid.Died:Connect(function()' there's no Humanoid inside a player, it's inside the character. That won't totally work

0

Ah. Yes. My mistake

0

after plrs[i] type .Character

0

and also change char to plrs[i].Character

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