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

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

Asked by 4 years ago

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

Log in to vote
3
Answered by 4 years ago

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)
0
Thank you so much! Highly appreciated. supercatsauthor 22 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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()
0
I tried that but it did not work still /: supercatsauthor 22 — 4y
0
Yeah because you can't use that on a server script, only for LocalScripts Gabe_elvin1226aclan 323 — 4y
Log in to vote
0
Answered by 4 years ago

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

0
'plrs[i].Humanoid.Died:Connect(function()' there's no Humanoid inside a player, it's inside the character. That won't totally work Gabe_elvin1226aclan 323 — 4y
0
Ah. Yes. My mistake JB_SuperGamer 165 — 4y
0
after plrs[i] type .Character JB_SuperGamer 165 — 4y
0
and also change char to plrs[i].Character JB_SuperGamer 165 — 4y

Answer this question