Hello, I have been trying to do this for 3 days now looking at others questions to see if I can piece something together what I am trying to do with this script is have a Lives counter for an entire team right now I’m just scripting the counter where it goes down when a player on that team dies
I have it where it detects if the player has died for the first time after firing the Event but it doesn’t keep searching it goes down from 5 to 4 but doesn’t keep dropping if i die again
I tried a Repeat loop but I think I did it wrong crashing my studio
local script
local Replicated = game.ReplicatedStorage
local player = game.Players.LocalPlayer
local SLife = Replicated.Stats.SLivesLeft
local ZLife = Replicated.Stats.ZLivesLeft
Replicated.Events.InGameStart.OnClientEvent:Connect(function() -- this works dont worry about this
wait(2)
player.Character.Humanoid.WalkSpeed = 16
end)
Replicated.Events.InGameStart.OnClientEvent:Connect(function() -- when the Game Starts i want it to check for a players Death
wait(2)
player.Character.Humanoid.Died:Connect(function()
print("death found")
if player.Team == game.Teams.Survivor then
Replicated.Events.LivesChange:FireServer(SLife)
elseif player.Team == game.Teams.Zombie then
Replicated.Events.LivesChange:FireServer(ZLife)
end
end)
end)
Server Script which works the counter goes down when the value is changed
I know that. Local Script is how I am checking, the issue isn’t that it doesn’t work it’s that it only checks the once and it’ll work then if i die again it wont it doesn’t keep searching after the first death for some reason
All the code you posted looks good, I’m not sure what isn’t working.
FYI, you can use Died on the server (and the client too). It might be a bit easier if you do it on the server though:
local ZLife -- SET TO VALUE
local SLife -- SET TO VALUE
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid", 5)
if not humanoid then --handling case where no humanoid is found.
return
end
humanoid.Died:Connect(function()
-- if you want the scores to be reduced only during games,
-- add an if statement here that checks if a game is happening
if player.Team == game.Teams.Survivor then
SLife.Value -= 1
elseif player.Team == game.Teams.Zombie then
ZLife.Value -= 1
end
end)
end)
end)
EDIT:
Whoops! I see what’s wrong with your code. When a Humanoid dies, it’s removed (along with the character). You need to get the humanoid of the NEXT/new character then reconnect the Died event. You can get the characters with Player.CharacterAdded.
For example, in the code above, every time a character is added I create a new connection to Humanoid.Died (for a the new Humanoid though).
local RS = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")
local Events = RS:WaitForChild("Events")
local LC = Events:WaitForChild("LivesChange")
local Stat = RS:WaitForChild("Stats")
local SLife = Stat:WaitForChild("SLivesLeft")
local ZLife = Stat:WaitForChild("ZLivesLeft")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
if player.Team == Teams.Survivor then
LC:FireServer(SLife)
elseif player.Team == Teams.Zombie then
LC:FireServer(ZLife)
end
end)
local RS = game:GetService("ReplicatedStorage")
local Events = RS:WaitForChild("Events")
local LC = Events:WaitForChild("LivesChange")
LC.OnServerEvent:Connect(function(plr, teamValue)
teamValue.Value -= 1
end)