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

How do I make this localscript wait for the character before it runs?

Okay so I got this and it works perfectly fine in testmode but it doesn't do anything at all in a normal server, does anyone know what is the problem?

player = game.Players.LocalPlayer
system = game.workspace.system


function onDeath()
system.playersalive.Value = system.playersalive.Value -1
player.CameraMode="Classic"
system.joindeadmsg.Value = player.Name .. " has died."
end

 player.Character.Humanoid.Died:connect(onDeath)

OUTPUT:

14:46:41.106 - Players.Player1.PlayerGui.maingui.ondead:10: attempt to index field 'Character' (a nil value)

14:46:41.106 - Stack Begin

14:46:41.107 - Script 'Players.Player1.PlayerGui.maingui.ondead', Line 10

14:46:41.108 - Stack End

2 answers

Use the :WaitForChild() event

Example

local Player = game.Players.LocalPlayer
local Character = Player:WaitForChild("Character")

3 comments

0

I did that, but it didn't work and now don't get output at all

0

its because its still waiting for the character

0

try using the :FindFirstChild() event if not WaitForChild

player = game.Players.LocalPlayer
player.CharacterAdded:wait()
system = game.workspace.system


function onDeath()
system.playersalive.Value = system.playersalive.Value -1
player.CameraMode="Classic"
system.joindeadmsg.Value = player.Name .. " has died."
end

player.Character.Humanoid.Died:connect(onDeath)

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