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.
Can’t you just parent them to the player object and set the values back to 100 every time they die?
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)
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.
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).
.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.