I am trying to make a function where everytime a player dies, a remote event is fired to the client side to tell the client to remove the player from the table that was created. For some reason it does not work at all.
Script:
local humanoid = character:WaitForChild("Humanoid")
player.CharacterAdded:Connect(function()
for i, v in pairs(game.Players:GetPlayers()) do
if inRoundC then
inRoundC:Disconnect()
print("disconnected")
end
end
local roundPlayer = table.find(playersInRound, player)
if roundPlayer then
diedRemote:FireClient(player) -- the problem lies here
table.remove(playersInRound, roundPlayer)
print("the player has been removed from the table")
end
end)
end)
end)
LocalScript:
for i, plr in pairs(game.Players:GetPlayers()) do
if plr ~= game.Players.LocalPlayer then
print("inserted the player")
table.insert(plrs, plr)
print(plrs)
end
end
game.Players.PlayerAdded:Connect(function(plr)
table.insert(plrs, plr)
end)
game.Players.PlayerRemoving:Connect(function(plr)
table.remove(plrs, plrs[plr])
end)
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
local function diedremote()
table.remove(plrs, plrs[plr])
print(plrs)
end
diedRemote.OnClientEvent:Connect(diedremote)
end)
end)
Basically the remote event should trigger when the player’s humanoid health is 0
Is this exactly how your indentation looks? I see a lot of functions having their end at the end of the code, which makes no sense to me.
And instead of using player.CharacterAdded to determine whether the player has died, using the Humanoid | Roblox Creator Documentation event would be better.
Could you also tell me what errors you’re getting?
I have tried the humanoid.Died function but it seems to bug out most of the time so i use the CharacterAdded event instead. The output does not really show any errors so I do think the way i wrote my script could be incorrect.
The end statements are making no sense to me, are you sure this is exactly how it is in roblox studio? Because from this code, I’m sure a lot of syntax errors would be popping up.
local humanoid = character:WaitForChild("Humanoid")
player.CharacterAdded:Connect(function()
for i, v in pairs(game.Players:GetPlayers()) do
if inRoundC then
print("disconnected")
inRoundC:Disconnect()
end
end
local roundPlayer = table.find(playersInRound, player)
if roundPlayer ~= nil then
diedRemote:FireClient(player) -- the problem lies here
for i,v in pairs(playersInRound) do
if player == v then
table.remove(playersInRound,i)
break
end
end
print("the player has been removed from the table")
end
end)
end)
end)
The last two statements are for the PlayerAdded and CharacterAdded event. I have tried to make it so when the player and the player’s character gets added it can trigger the event when it is fired in the server script
why’d u put the remote event in the function that’s meant to be called when the remote event is fired? also, did u try printing out the value of roundPlayer or player to see what’s going on?
table.find() would just return the position of the element, not the value of the element itself.
i’m a bit curious why the client would have access to the table. that would mean that exploiters can just manipulate the table (delete everything in it, add some things etc.)
I forgot to mention that I want to remove a player from another table i have created on the client. I would like to make it so when the event fires it removes the player from the client side table. But I appreciate the help
the localscript on startergui
local plrs = {}
for i, plr in pairs(game.Players:GetPlayers()) do
if plr ~= game.Players.LocalPlayer then
print("inserted the player")
table.insert(plrs, plr)
print(plrs)
end
end
game.Players.PlayerAdded:Connect(function(plr)
table.insert(plrs, plr)
end)
game.Players.PlayerRemoving:Connect(function(plr)
table.remove(plrs, plrs[plr])
end)
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
local function diedremote()
table.remove(plrs, plrs[plr])
print(plrs)
end
diedRemote.OnClientEvent:Connect(diedremote)
end)
end)
For more information, it is a spectate system and whenever a player dies on the server side, the server script gets the information, fires the event to the client and tells the client to remove the local player from the table (in the local script)
The problem I have here is actually how to define the player in my local script.