Add instance when player dies

Hello! Im making a game where an instance gets added when they join, and the same instance gets added again when they die and respawn. I tried .Died and it didn’t work. I don’t know why. Can someone help me? I would really appreciate it.

Thank you.

game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local stamina = Instance.new("IntValue")
	stamina.Parent = char
	stamina.Name = "Stamina"
	stamina.Value = 100
	
	local stamina = Instance.new("IntValue")
	stamina.Parent = char
	stamina.Name = "BlockingStamina"
	stamina.Value = 100
	
	plr.Character.Humanoid.Died:Connect(function()
		local stamina = Instance.new("IntValue")
		stamina.Parent = char
		stamina.Name = "Stamina"
		stamina.Value = 100

		local stamina = Instance.new("IntValue")
		stamina.Parent = char
		stamina.Name = "BlockingStamina"
		stamina.Value = 100
	end)
end)
2 Likes
  1. Can’t you just parent them to the player object and set the values back to 100 every time they die?
  2. Player.CharacterAdded fires everytime they respawn so you can do this:
local function CharAdded(plr) -- function to call on respawn
local char = player.Character
-- do what you want when player respawns
end

game.Players.PlayerAdded:Connect(function(plr)
-- add instances
if plr.Character then CharAdded(plr) end -- if char exists then call function to prevent code not firing CharacterAdded
plr.CharacterAdded:Connect(function(char)
CharAdded(plr) -- fires every time player respawns
end)
end)
1 Like
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local stamina = Instance.new("IntValue", char)
		stamina.Name = "Stamina"
		stamina.Value = 100

		local stamina = Instance.new("IntValue", char)
		stamina.Name = "BlockingStamina"
		stamina.Value = 100
	end)
end)
2 Likes

I think the issue might be that the Humanoid.Died event happens as soon as the player dies, and the instances would be added to the player before it is respawned. Like PoppyandNeivaarecute has done, I’d use the plr.CharacterAdded event. I would do it slightly differently though:

game.Players.PlayerAdded:Connect(function(plr)
	local function addInstances(character) -- A function to add the instances
		if character:FindFirstChild("Stamina") == nil and character:FindFirstChild("BlockingStamina") == nil then -- Prevents the instances from being added twice for whatever reason
			local stamina = Instance.new("IntValue")
			stamina.Parent = character
			stamina.Name = "Stamina"
			stamina.Value = 100

			local blockingStamina = Instance.new("IntValue") -- I renamed this variable, it used to be the second "stamina" variable in your code
			blockingStamina.Parent = character
			blockingStamina.Name = "BlockingStamina"
			blockingStamina.Value = 100
		end
	end
	if plr.Character then addInstances(plr.Character) end -- if the character already exists, adding the instances
	plr.CharacterAdded:Connect(addInstances) -- whenever the character is added from now on, the instances will be added
end)

In my experience, this type of system has worked for me. I’m sure there is still a better way to do it.

2 Likes

Hey!
I think you might be overcomplicating the issue or confusing it! Here’s what I would suggest in the various scenarios that I think you might be trying to achieve!

Before I get into the Technical stuff, your Code is working and It currently Adds an IntValue instance to the Character not the Player Which means that it will only make a new Instance of IntValue when the player joins. After this, you have a .Died Event that basically Makes 2 More Instances of these and Adds them to the Player (This is Working)

If your Question is:
I Want to Add an IntValue to the Character when They Join the game and I want to Add it Everytime they Respawn (One of Each Type).

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local stamina = Instance.new("IntValue")
		stamina.Parent = char
		stamina.Name = "Stamina"
		stamina.Value = 100

		local stamina = Instance.new("IntValue")
		stamina.Parent = char
		stamina.Name = "BlockingStamina"
		stamina.Value = 100

	end)
end)

What this Essentially Does is that every time a Character spawns in, it Adds these Instances to it. (Previous Ones Get Removed on Death)

2 Likes

Don’t reference the dead character, wait until the new one is respawned.

game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local stamina = Instance.new("IntValue")
	stamina.Parent = char
	stamina.Name = "Stamina"
	stamina.Value = 100
	
	local stamina = Instance.new("IntValue")
	stamina.Parent = char
	stamina.Name = "BlockingStamina"
	stamina.Value = 100
	
	plr.Character.Humanoid.Died:Connect(function()
		local newChar = plr.CharacterAdded:Wait()

		local stamina = Instance.new("IntValue")
		stamina.Parent = newChar
		stamina.Name = "Stamina"
		stamina.Value = 100

		local stamina = Instance.new("IntValue")
		stamina.Parent = newChar
		stamina.Name = "BlockingStamina"
		stamina.Value = 100
	end)
end)

I do think its better to just use .CharacterAdded cause this is just similar to it’s purpose.

1 Like

.Chatacter on player instance is a property that changes whenever character dispawns which in that case it will change to nil until the player respawns which turns to Model object or well the character in simple words, referencing is not the problem.

1 Like
local Players = game:GetService("Players")

local function CreateValue(Name : string) -- just like it cleaner
    local temp = Instance.new("IntValue")
    temp.Name = Name
    temp.Value = 100
    
    return temp
end

local Connections = {}

Players.PlayerAdded:Connect(function(plr)
    local Stamina = CreateValue("Stamina")
    local BlockingStamina = CreateValue("BlockingStamina")

    Connections[plr] = {
        Values = {
            Stamina = Stamina,
            BlockingStamina = BlockingStamina
        },
        CharacterAdded = plr.CharacterAdded:Connect(function(char : Model)
            Stamina.Parent = char
            BlockingStamina.Parent = char
        end),
        CharacterRemoving = plr.CharacterRemoving:Connect(function(char : Model)
            Stamina.Parent = nil
            BlockingStamina.Parent = nil
        end)
    }
end)

Players.PlayerRemoving:Connect(function(plr)
    local found = Connections[plr]

    if found then -- just incase
        Connections[plr] = nil

        found.CharacterAdded:Disconnect()
        found.CharacterRemoving:Disconnect()
        
        local Values = found.Values

        Values.Stamina:Destroy()
        Values.BlockingStamina:Destroy()
        
        table.clear(Values)
        table.clear(found)
    end
end)
1 Like

Thank you everyone for helping me!! I only had to pick one solution though, but if I could I would mark all of yours as solutions as well. Thank you.

If you really just want to add values in the character when it respawns, no scripting is required.
Just add your values in StarterCharacterScripts

that’s true but I thought that he wanted the same Instance to follow to every new Character Model

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.