Remote event not triggering

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.

Especially in this part, the PlayerAdded function has the diedremote function INSIDE it, and the end statements at the end of your code.

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)

Might I ask what the last two end statements are for? And does it print out that they have been removed in the server script?

Yes, this is how it is in roblox studio, it does not trigger the event but at the same time it displays no signs of error.

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

Does it print out anything at all?

It prints stuff on the server side yes. But on the client side once the event should have triggered and should have printed the plr table, it has not.

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.

Is this the whole script? It looks like it’s missing the playerAdded connection somewhere.

I think you’re in the wrong thread

Sorry I made a post but I uploaded the script in the wrong area

2 Likes

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.

move this out of the diedremote function.

It is not directly in the function of course, but instead it is connected to the CharacterAdded event. Where should I call the function instead?

PlayerAdded doesn’t fire in local scripts.

PlayerRemoving does however, so to maintain an array of players in a local script you would do the following.

local Players = game:GetService("Players")
local PlayersArray = Players:GetPlayers()

Players.PlayerRemoving:Connect(function(Player)
	PlayersArray = Players:GetPlayers()
end)