I’m making a control point script and when a player touches the point a humanoid.died and touchended event is connected. When the player exists the point the touchened event fires and disconnects the humanoid.died event.
My issue is when two players are on the control point and a player dies the humanoid.died event runs fine, but when the next person dies the humanoid.died event will not run.
My whole code: (This script is a server-sided script in ServerScriptService)
local homeTeam = {}
local awayTeam = {}
local function playerSubtraction(part, humanoid)
if part.Name == "HumanoidRootPart" and part.Parent:FindFirstChild("Humanoid").Health > 0 and table.find(homeTeam, part.Parent.Name) then
print(part.Parent.Name, "Removed")
table.remove(homeTeam, table.find(homeTeam, part.Parent.Name))
end
end
local function playerAddition(part)
if part.Name == "HumanoidRootPart" and part.Parent:FindFirstChild("Humanoid").Health > 0 and not table.find(homeTeam, part.Parent.Name) then
print(part.Parent.Name, "Added")
table.insert(homeTeam, part.Parent.Name)
local function diedOnPoint()
print(part.Parent.Name, "Died & Removed")
table.remove(homeTeam, table.find(homeTeam, part.Parent.Name))
end
local humanoidDeath = part.Parent.Humanoid.Died:Connect(diedOnPoint)
local touchEnded = workspace.hillHitbox.TouchEnded:Connect(function(part)
humanoidDeath:Disconnect()
playerSubtraction(part)
end)
end
end
workspace.hillHitbox.Touched:Connect(playerAddition)
I’ve tried wrapping the main touch event in a spawn function
I’ve tried wrapping the humanoid.died event in a spawn function
How would I go about making each players humanoid.died event individual? I believe every other humanoid.died event is being disconnected after 1 player dies.
Problem is in your touchended event, It never checks if it’s the same humanoidrootpart
Also you never disconnect the touchended event so that could be a memory leak.
local function playerAddition(part)
if part.Name == "HumanoidRootPart" and part.Parent:FindFirstChild("Humanoid").Health > 0 and not table.find(homeTeam, part.Parent.Name) then
print(part.Parent.Name, "Added")
table.insert(homeTeam, part.Parent.Name)
local function diedOnPoint()
print(part.Parent.Name, "Died & Removed")
table.remove(homeTeam, table.find(homeTeam, part.Parent.Name))
end
local humanoidDeath = part.Parent.Humanoid.Died:Connect(diedOnPoint)
local touchEnded
touchEnded = workspace.hillHitbox.TouchEnded:Connect(function(part2)
if part2 == part then
humanoidDeath:Disconnect()
playerSubtraction(part)
touchEnded:Disconnect()
end
end)
end
end
I put in what you had written out but I’m still experiencing some issues.
When initially testing with 2 players it works fine when both of them walk onto the point, but when the first person resets (Player1 in this case) it also removed Player2 from the point even though he didn’t move.
Plus why is Player1 getting removed and added back after they’ve already died?
Blue & red is the players walking onto the point, the yellow is moment they reset.
Okay I figured out the issue and your script works perfectly, thank you. Its just the touchended event not working properly lol. Whenever a player resets there is a chance the touchended event will fire and screw everything up, majority of the time testing your script it was working as I wanted it.
I started to print the players health after they reset to see if they were even alive when the touchended event happens and it prints 100. Can’t even make this up.
Here is the full fix of the script if anyone may need it in the future.
The main fix was just to give the script a task.wait() to make sure the player was properly dead.
local homeTeam = {}
local awayTeam = {}
local function playerSubtraction(part)
if part.Name == "HumanoidRootPart" and part.Parent:FindFirstChild("Humanoid").Health > 0 and table.find(homeTeam, part.Parent.Name) then
print(part.Parent.Name, "Removed")
table.remove(homeTeam, table.find(homeTeam, part.Parent.Name))
end
end
local function playerAddition(part)
if part.Name == "HumanoidRootPart" and part.Parent:FindFirstChild("Humanoid").Health > 0 and not table.find(homeTeam, part.Parent.Name) then
print(part.Parent.Name, "Added")
table.insert(homeTeam, part.Parent.Name)
local function diedOnPoint()
print(part.Parent.Name, "Died & Removed")
table.remove(homeTeam, table.find(homeTeam, part.Parent.Name))
end
local humanoidDeath = part.Parent.Humanoid.Died:Connect(diedOnPoint)
local touchEnd
touchEnd = workspace.hillHitbox.TouchEnded:Connect(function(part2)
if part2 == part then
task.wait()
touchEnd:Disconnect()
humanoidDeath:Disconnect()
playerSubtraction(part)
end
end)
end
end
workspace.hillHitbox.Touched:Connect(playerAddition)