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

Why am I getting a "disconnected event from exception" when I attempt to run this code?

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!

3 comments

0

On line 11, try putting 'game.Players.PlayerAdded:connect(killPlayer())' -- I added the parenthesis after the 'killPlayer' function.

0

Keep in mind, I put this in a comment because I am not 100% sure that is the issue. (I cannot test it at the moment)

0

Nope, that doesn't work. I still get a disconnected event because of exception. qq

1 answer

✓ Accepted answer

Error Message

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.


Problems

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

Correct Program

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)

Lesson

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.

1 comment

1

Recheck the codes you did: This code 'player.Character:WaitForChild("Humanoid")Health = 0' has an error that will cause an error, because you forgot to put the '.' between 'Health' and 'WaitForChild("Humanoid")' to identify the property selected, thus, the code thinks that 'Health' is a global identifier because 'Health = 0'. :P

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