Last one standing system not working

Hey! So, I’m trying to make a system, that makes a player win, if they’re the last one standing. It then displays it on a GUI, as well as the other 4 players (1st-5th places).

It’s meant to work through an “Alive” team.

ServerScript:

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local RS = game:GetService("ReplicatedStorage")
local EndGameEvent = RS:WaitForChild("EndGame")
local SS = game:GetService("ServerStorage")
local PlacementsFolder = SS:WaitForChild("Placements")
local FirstValue = PlacementsFolder:WaitForChild("1st")
local SecondValue = PlacementsFolder:WaitForChild("2nd")
local ThirdValue = PlacementsFolder:WaitForChild("3rd")
local FourthValue = PlacementsFolder:WaitForChild("4th")
local FifthValue = PlacementsFolder:WaitForChild("5th")

local DataPlayers = {}

local function addPlayer(Player)
	table.insert(DataPlayers, Player.Name)
end
local function removePlayer(Player)
	local tablePlayer = table.find(DataPlayers, Player.Name)
	table.remove(DataPlayers, tablePlayer)
end

print("before player added")
Players.PlayerAdded:Connect(function(Player)
	print("after player added")
	Player.CharacterAdded:Connect(function()
		print("after character added")
		if Player.Team == Teams.Alive then
			addPlayer(Player)
			print("after addPlayer")
			print(DataPlayers)
			print("after add DataPlayers")
		elseif Player.Team == Teams.Dead then
			removePlayer(Player)
			print("after removePlayer")
			print(DataPlayers)
			print("after remove DataPlayers")
			if #DataPlayers == 4 then
				FifthValue.Value = Player.Name
			elseif #DataPlayers == 3 then
				FourthValue.Value = Player.Name
			elseif #DataPlayers == 2 then
				ThirdValue.Value = Player.Name
			elseif #DataPlayers == 1 then
				SecondValue.Value = Player.Name
				local firstPlayer = table.unpack(DataPlayers, 1)
				FirstValue.Value = firstPlayer.Name
				EndGameEvent:FireAllClients(firstPlayer)
				print("SecondValue: "..SecondValue.Value)
				print("FirstValue: "..FirstValue.Value)
			end
			print("after #DataPlayers checks")
		end
	end)
end)

LocalScript:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local PlayerGUI = LocalPlayer.PlayerGui
local RS = game:GetService("ReplicatedStorage")
local Event = RS:WaitForChild("EndGame")

Event.OnClientEvent:Connect(function(Player)
	local GUI = PlayerGUI:WaitForChild("EndAnnouncement")
	local HolderFrame = GUI:WaitForChild("Holder")
	local Text = HolderFrame:WaitForChild("Announcement")
	
	Text.Text = "<b>"..Player.Name.."</b> WON THE GAME!"
end)
1 Like

What’s wrong with it is there any specific thing that isn’t happening?

The text doesn’t change at all, but I believe that the prints do outprint. Might be an issue with the event, not sure.

There is no Player argument while receiving a ClientEvent that was fired from FireAllClients.

So, your function should look like this:

Event.OnClientEvent:Connect(function()
	local GUI = PlayerGUI:WaitForChild("EndAnnouncement")
	local HolderFrame = GUI and GUI:WaitForChild("Holder")
	local Text = HolderFrame and HolderFrame:WaitForChild("Announcement")
	
	Text.Text = "<b>"..LocalPlayer.Name.."</b> WON THE GAME!"
end)

Your scripts are pretty much bad written, you could do something better, especially for the server one.

Local Script

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local LocalPlayer = Players.LocalPlayer
local PlayerGUI = LocalPlayer:WaitForChild("PlayerGui")

local Event = RS:WaitForChild("EndGame")

Event.OnClientEvent:Connect(function()
	local GUI = PlayerGUI and PlayerGUI:WaitForChild("EndAnnouncement")
	local HolderFrame = GUI and GUI:WaitForChild("Holder")
	local Text = HolderFrame and HolderFrame:WaitForChild("Announcement")
	
	if Text then
		Text.Text = "<b>"..LocalPlayer.Name.."</b> WON THE GAME!"
	end
end)

Server Script
Delete the “st” “nd” “rd” “th” from your placements value name, so it allow you to do something easier in your script.

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")

local EndGameEvent = RS:WaitForChild("EndGame")
local PlacementsFolder = SS:WaitForChild("Placements")

local DataPlayers = {}

local function addPlayer(Player)
	table.insert(DataPlayers, Player.Name)
end

local function removePlayer(Player)
	local tablePlayer = table.find(DataPlayers, Player.Name)
	table.remove(DataPlayers, tablePlayer)
end

local function checkValues(Player)	
	if #DataPlayers > 2 then
		local CurrentValue = PlacementsFolder:FindFirstChild(tostring(#DataPlayers))		
		CurrentValue.Value = Player.Name
	elseif #DataPlayers == 2 then
		local FirstValue = PlacementsFolder:FindFirstChild("1")
		local SecondValue = PlacementsFolder:FindFirstChild("2")
		local firstPlayer = table.unpack(DataPlayers, 1)
		
		SecondValue.Value = Player.Name
		FirstValue.Value = firstPlayer.Name
		EndGameEvent:FireAllClients(firstPlayer)
	end
end

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		if Player.Team == Teams.Alive then
			addPlayer(Player)
		elseif Player.Team == Teams.Dead then
			checkValues(Player)
			removePlayer(Player)
		end
	end)
end)
1 Like

Thanks for the script. Though, it seems that the event still doesn’t work. Any idea what might be wrong?

I put some prints to realize what’s wrong, but I’m still not pretty sure.

For example here:

		print("before event fire")
		EndGameEvent:FireAllClients(FirstValue.Value)
		print("after event fire")

Only the before print gets outprinted, the after one doesn’t. (It didn’t print with (firstPlayer) or just () either)

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local LocalPlayer = Players.LocalPlayer
local PlayerGUI = LocalPlayer:WaitForChild("PlayerGui")

local Event = RS:WaitForChild("EndGame")

print("before onEvent")
Event.OnClientEvent:Connect(function(FirstValue)
	local targetPlayer = Players:FindFirstChild(FirstValue)
	print(targetPlayer.Name)
	print("after onEvent")
	local GUI = PlayerGUI:WaitForChild("EndAnnouncement")
	local HolderFrame = GUI:WaitForChild("Holder")
	local Text = HolderFrame:WaitForChild("Announcement")
	
	print("before Text")
	if Text then
		Text.Text = targetPlayer.Name
		print("after Text change")
	end
end)

Here, only the before onEvent print gets outprinted, nothing else.

Note that there aren’t any errors.

Are you sure the event is found by the script ?
Did you tried to fire it without argument and see if it work ?

Make sure names are same from the script and the remote event, and check if there is a space somewhere in the remote name, before or after the text.

1 Like

Yes, the RemoteEvent is indeed found by the script.
Yep, I tried firing it without any arguments, but as I already said, that didn’t work either.

Alright, so.

I made some rework and it seems that it works now. Thanks though.

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