Infinite yield when waiting for "LocalPlayer" on a Server Side script

I’m trying to create a countdown timer. This is being made in a server script in ServerScriptService.

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

function Rounds()
	PlayerGui.MatchHUD.Countdown.Text = "10"
	for i = 1, 10, 1 do
		PlayerGui.MatchHUD.Countdown.Text = tostring(i)
		wait(1)
	end
	SelectPart()
	return
end

When I run this, I get this warning:

Anyone know how to fix this while keeping it in a Server Script? Many answers / tutorials I’ve watched just said to switch to local, but I need this to be in a Server Script.

1 Like

You cant get the local player from server scripts

You can’t access PlayerGui or LocalPlayer from the server, you should try to use a RemoteEvent to do what you’re trying to do here

You most likely could make a IntValue in Workspace, ReplicatedStorage and then you change the value of the IntValue to the countdown using the SS-Script and just detect from the Client if the value has been changed, if so then you change the text

1 Like

But I need all players to see the function being run, not only the one who it’s affecting. So I need it to be in a Server Script. Here’s my full script:

local PlatesFolder = game.Workspace.Game.Map.Plates
local Floor = game.Workspace.Game.Map.Map.Floor
local Countdown = game.StarterGui.MatchHUD.Countdown
local Plates = PlatesFolder:GetChildren()
local PlayerGui = game.Players:WaitForChild("LocalPlayer").PlayerGui

function SelectPart()
	local randomPlate = Plates[math.random(1, #Plates)]

	-- [MAKING THE ATTACKS]
	local function LavaAttack()
		-- In the future, I'd turn this into a tween
		-- Set player health to 0
		print("LavaAttack")
	end
	local function SpinAttack()
		-- In the future, I'd turn this into a tween
		print("SpinAttack")
	end
	local function Shrink()
		-- In the future, I'd turn this into a tween
		print("Shrink")
	end
	local function Cage()
		-- In the future, I'd turn this into a tween
		print("Cage")
	end

	-- [Play a random attack on this plate]
	local Functions = {Cage, Shrink, SpinAttack, LavaAttack}
	local ChosenFunction = Functions[math.random(1, #Functions)]

	ChosenFunction()
end

function Rounds()
	PlayerGui.MatchHUD.Countdown.Text = "10"
	for i = 1, 10, 1 do
		PlayerGui.MatchHUD.Countdown.Text = tostring(i)
		wait(1)
	end
	SelectPart()
	return
end

Rounds()

You can’t get the local player from the server, the tooltip is literally, the client this localscript is running for.

what you’d do is use a remote event and modify the count down on each client.
RemoteEvent | Documentation - Roblox Creator Hub

Try to read through this or watch some yt vids on remote events

you’d use a remote event,
Like so:

--other variables
--delete playergui
local remote = --add remotevent
function rounds()
  remote:FireAllClients(10)
  task.wait(10) -- change this to something more in sync
  SelectPart()
--no need for return
end

--in your client script
local player = game.Players.LocalPlayer
local playerUI = player.PlayerGui
local label = playerUI.MatchHUD.Countdown.Text
local remote -- your remote

remote.OnClientEvent:Connect(function(time: number)
  label.Text = time
  for i=time, 0, -1 do
     label.Text = i
     task.wait(1)
  end
end)

and boom, now it should work.

Hope this helps!

1 Like

Where do I put my SelectPart() function? Client or server?

server, everything after " --in your client script" should be in the client and everything before it on the server.

1 Like

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