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

Script isn't calling this CharacterAdded function?

Asked by
R_alatch 394 Moderation Voter
6 years ago
Edited 6 years ago

I have a script where when a player dies, it drops a gun at their HumanoidRootPart's position. The problem is, it's not calling the "HandleSheriff" function when they die. I added a print after the CharacterAdded function and it never prints when I reset my character.

Code:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        handlesheriff(player, character)
    end)

    if player.Character and player.Character.Parent then
        handlesheriff(player, player.Character)
    end
end)

function handlesheriff(bloxxedplayer, character)
    local humanoid = character:FindFirstChild("Humanoid")

    if humanoid then
        humanoid.Died:Connect(function()
            if bloxxedplayer == sheriff then
                local backpack = bloxxedplayer:FindFirstChild("Backpack")
                local root = character:FindFirstChild("HumanoidRootPart")

                if backpack and root then
                    humanoid:UnequipTools()
                    backpack:ClearAllChildren()

                    droptaser(root.Position)
                end
            end

            local tag = humanoid:FindFirstChild("creator")

            if tag then
                local wrongdoer = players:GetPlayerByUserId(players:GetUserIdFromNameAsync(tag.Value))

                if wrongdoer.Character and wrongdoer.Character.Parent then
                    local wrongdoerhumanoid = wrongdoer.Character:FindFirstChild("Humanoid")

                    if wrongdoerhumanoid then
                        wrongdoerhumanoid.Health = 0
                    end
                end
            end
        end)
    end
end

3 answers

Log in to vote
0
Answered by 6 years ago

Ok, i'm not completely clear on what exactly the problem is, but i noticed two mistakes.

  1. you use "player.PlayerAdded" is player equal to game.Players? 2.PlayerAdded is fired when a player joins for the first time, not every time the players resets.

Use

http://wiki.roblox.com/index.php?title=API:Class/Player/CharacterAppearanceLoaded

instead.

Ad
Log in to vote
0
Answered by
Prestory 1395 Moderation Voter
6 years ago

Try using this function and swap it out

game.Players.PlayerAdded:connect(function(player)
    --type in code here
end)

0
what does it say in the output? frederikhome 44 — 6y
Log in to vote
0
Answered by 6 years ago

Hmm. It's unclear at face value what exactly is going wrong with your code.There are some things that are noticeably missing from your snippet. My guess is that if something is not printing from CharacterAdded then the event is not being connected to.

The first is the players variable that you use on lines 31, and 32. You also did not specify how sheriff and bloxxer from line 39 and 54 are obtained.

What I would try first is to make sure that players is set correctly.

local players = game:GetService("Players")

This is what it should look like. PlayerAdded comes from the Players service.

Also the following code I don't think is correct from line 53.

if tag and tag.Value and tag.Value.Parent == players then

The Value won't have a parent (it's just a string with a player's name I am guessing). You made the mistake of thinking that it will point to a Player (who's parent would be in fact Players like you guessed).

Here's how I would code it to retrieve the player using the String value of their username. Again creator could actually be the userId of the player and that would work better. I don't know because you didn't tell me.

if tag then -- No need to check for the Value either.
    local wrongdoer = players:GetPlayerByUserId(players:GetUserIdFromNameAsync(tag.value))
    if wrongdoer and wrongdoer ~= bloxxer and bloxxedplayer ~= bloxxer then
        if wrongdoer.Character and wrongdoer.Character.Parent then
            local wrongdoerhumanoid = wrongdoer.Character:FindFirstChild("Humanoid")
0
I didn't post the entire code. If you want me to, I can update it. R_alatch 394 — 6y
0
That's alright. I think we just need to see the bit of code that talks about players. You can also remove functions like dropnetgun because that I do not believe is part of the problem. User#18718 0 — 6y

Answer this question