function killPlayer() local player = game.Players:FindFirstChild("Player") if player ~= nil then local blah = game.Players:GetPlayerFromCharacter(player) blah.Humanoid.Health = 0 end end game.Players.PlayerAdded:connect(killPlayer)
I am trying to kill a player when they join the game.
This is just a little test. I'm experimenting in order to learn Lua a bit. I don't see the problem here, please help. It would be greatly appreciated, since I am quite the beginner!
The error you're getting is basically saying that killPlayer
errored, which means that the PlayerAdded
event is going to ignore it from then on.
You don't care about the particular player named "Player", probably -- you should probably be merging blah
and player
.
Actually, blah
(or what you mean to be blah
) is a parameter of the PlayerAdded event:
function newPlayer( player ) -- player is the Player object who just joined end game.Players.PlayerAdded:connect( newPlayer )
Note that Player objects don't have a Humanoid in them. It's in their Character, but their Character won't be there the moment the player appears (it takes a little time to spawn) so we should wait for that to happen:
repeat wait() until player.Character
Now we can make a simple killPlayer
function that kills the player's Character's Humanoid:
function killPlayer( player ) player.Character:WaitForChild("Humanoid").Health = 0 -- EDITED: Fixed typo (was missing . before Health) end
Putting it all together:
function killPlayer( player ) player.Character:WaitForChild("Humanoid").Health = 0 -- EDITED: Fixed typo (was missing . before Health) end function newPlayer( player ) repeat wait() until player.Character killPlayer( player ) end game.Players.PlayerAdded:connect(newPlayer)
Don't just write things down because they look right.
Every little piece of a program has a purpose, down to the quotes. Make sure you know why you picked something -- if you don't know, you shouldn't have picked what you did.