How to have the share table with server and local script

In short, I have a table that contains all players, if someone dies then they are removed. I have a life system, so if they die and they are not on 0 lives then they do not get removed.

I have a intvalue called “LivesCount” and it stores how many lives does a player have. If I put:

if Lives.Value == 0 then
   -- die script
end

on a server script then that means everyone’s lives has to be 0 to make the code work, so I transferred the table to local script via remote event. It works, but the problem is that whenever I remove the player in the table with the local script, it wouldn’t be applied to the server script (I won’t be talking on why I need it on a server script now because it would be kinda long and hard to explain).

I’m a missing something here? Or is there a much simpler way to do this? Please help me with my problem and I would be forever grateful.

Here’s the code I have currently:
Server script:

for x, player in pairs (plrs) do
				if player then

					character = player.Character

					if not character then
						-- Left the game
						table.remove(plrs, x)
					else
						if character:FindFirstChild("GameTag") then
							-- They are still alive
							print(player.Name.. "is still in the game!")
						else
							-- They are dead lol
							game.ReplicatedStorage:WaitForChild("LivesButThisTimeItWillWok"):FireClient(player, plrs, x)
							print(plrs)
						end
					end
				else
					table.remove(plrs, x)
					player.Team = game.Teams["Not Playing"]
					print(player.Name.." has been removed!")
				end

Focus on the “they are dead lol” part of script

Local script:

game:GetService("ReplicatedStorage"):WaitForChild("LivesButThisTimeItWillWok").OnClientEvent:Connect(function(plrs, x)
	if LivesCount.Value == 0 then
		table.remove(plrs, x)
		player.Team = game.Teams["Not Playing"]
		print("removed?")
	else
		print("Life not 0")
	end
end)

Thank you for reading

I think events would be good for getting and transferring data.

I did that already, but for me remote event does not work for me

Do you still have the previous code that you used for the remotes?

Where did you define ‘plrs’ table from? May I see the previous line of the code

i defined the “plrs” table from the server script

Here’s a code of it:

local plrs = {}

	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) -- Adds Players in the table
		end
	end

I’m not sure if this is a related thing for me to say, but doesn’t this only get the players once?

Well here’s the thing. You are basically passing plrs table info to the client as a new variable. You can’t change (remove in this case) what’s inside it from the client.

You would have to use RemoteFunction to do changes from the server.

ah ok, I completely forgotten about remote functions

Code example:

Server:

local LivesButThisTimeItWillWork = -- make this a remote function
local resultOfCheck = LivesButtThisTimeItWillWork:InvokeClient(player, plrs, x)
if resultOfCheck == true then -- checks if the returned value was true
	table.remove(plrs, x) -- remove the plr here from the server
end

Client:

LivesButThisTimeItWillWork.OnClientInvoke = function(plrs, x)
	if LivesCount.Value == 0 then
		-- still do your stuff here but no need to remove the plr from plrs table yet
		return true -- returns true value
	else
		print("Life not 0")
	end
end)

Check CKScripts’ reply about RemoteFunctions. Provided you with an example as well.

Thanks! I’ve been working on my live system for a couple of days now and now it works :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.