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

Script isn't calling this CharacterAdded function?

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

Try using this function and swap it out

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

1 comment

0

what does it say in the output?

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")

2 comments

0

I didn't post the entire code. If you want me to, I can update it.

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.

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