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

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

Asked by 9 years ago
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!

0
On line 11, try putting 'game.Players.PlayerAdded:connect(killPlayer())' -- I added the parenthesis after the 'killPlayer' function. Necrorave 560 — 9y
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) Necrorave 560 — 9y
0
Nope, that doesn't work. I still get a disconnected event because of exception. qq dirty_catheter 7 — 9y

1 answer

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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
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 TheeDeathCaster 2368 — 9y
Ad

Answer this question