Getting player from server script?

Ok, so I have a round system, but I need a better way to get the player. Right now, it looks like this:

game.Players.PlayerAdded:Connect(function(player)
       -- whatever
end)

but I’m going to take a guess that if i put my round function it, the round is going to fire everytime a player joins which may interrupting to other players that were playing before. How can I fix this?

if you MUST, here is the full code:

game.Players.RespawnTime = math.huge
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(
	1.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local waitTime = 4.5
local extraTime = 2.5
local price = 365

game.ReplicatedStorage.BuyMoreTime.OnServerEvent:Connect(function(player)
	if player.leaderstats.Coins.Value >= price then
		player.leaderstats.Coins.Value -= price
		waitTime += extraTime
		game.ReplicatedStorage.DisplayTimer:FireClient(player)
		task.wait(150)
		waitTime -= extraTime
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	if player.MembershipType == Enum.MembershipType.Premium then
		waitTime += 1
	end
	local function round()
		task.wait(3.5)
		local random = math.random(1, 4)
		if random == 1 then
			player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Text = "Red"
			player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.TextColor3 = Color3.fromRGB(255, 0, 0)
			local t = ts:Create(player.PlayerGui:WaitForChild("ScreenGui").ReadyUp, ti, {TextTransparency = 1})
			t:Play()
			t.Completed:Connect(function()
				player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Visible = false
			end)
			task.wait(waitTime)
			for i, v in pairs(workspace.Map:GetChildren()) do
				if v.Name ~= player.PlayerGui.ScreenGui.ReadyUp.Text and v:IsA("Part") then
					v.Transparency = 0.7
					v.CanCollide = false
				end
			end
			task.wait(math.sqrt(workspace.Gravity / player.Character.Torso.Position.Y))
			if player.Character.Humanoid.Health ~= 0 then
				if player.MembershipType == Enum.MembershipType.None then
						player.leaderstats.Coins.Value += 50
					elseif player.MembershipType == Enum.MembershipType.Premium then
						player.leaderstats.Coins.Value += 75
					end
			else
				if player.MembershipType == Enum.MembershipType.None then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 50 / 2
					end
				elseif player.MembershipType == Enum.MembershipType.Premium then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 75 / 5
					end
				end
			end
		elseif random == 2 then
			player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Text = "Yellow"
			player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.TextColor3 = Color3.fromRGB(255, 255, 0)
			local t = ts:Create(player.PlayerGui:WaitForChild("ScreenGui").ReadyUp, ti, {TextTransparency = 1})
			t:Play()
			t.Completed:Connect(function()
				player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Visible = false
			end)
			task.wait(waitTime)
			for i, v in pairs(workspace.Map:GetChildren()) do
				if v.Name ~= player.PlayerGui.ScreenGui.ReadyUp.Text and v:IsA("Part") then
					v.Transparency = 0.7
					v.CanCollide = false
				end
			end
			task.wait(math.sqrt(workspace.Gravity / player.Character.Torso.Position.Y))
			if player.Character.Humanoid.Health ~= 0 then
				if player.MembershipType == Enum.MembershipType.None then
					player.leaderstats.Coins.Value += 50
				elseif player.MembershipType == Enum.MembershipType.Premium then
					player.leaderstats.Coins.Value += 75
				end
			else
				if player.MembershipType == Enum.MembershipType.None then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 50 / 2
					end
				elseif player.MembershipType == Enum.MembershipType.Premium then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 75 / 5
					end
				end
			end
		elseif random == 3 then
			player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Text = "Green"
			player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.TextColor3 = Color3.fromRGB(0, 255, 0)
			local t = ts:Create(player.PlayerGui:WaitForChild("ScreenGui").ReadyUp, ti, {TextTransparency = 1})
			t:Play()
			t.Completed:Connect(function()
				player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Visible = false
			end)
			task.wait(waitTime)
			for i, v in pairs(workspace.Map:GetChildren()) do
				if v.Name ~= player.PlayerGui.ScreenGui.ReadyUp.Text and v:IsA("Part") then
					v.Transparency = 0.7
					v.CanCollide = false
				end
			end
			task.wait(math.sqrt(workspace.Gravity / player.Character.Torso.Position.Y))
			if player.Character.Humanoid.Health ~= 0 then
				if player.MembershipType == Enum.MembershipType.None then
					player.leaderstats.Coins.Value += 50
				elseif player.MembershipType == Enum.MembershipType.Premium then
					player.leaderstats.Coins.Value += 75
				end
			else
				if player.MembershipType == Enum.MembershipType.None then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 50 / 2
					end
				elseif player.MembershipType == Enum.MembershipType.Premium then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 75 / 5
					end
				end
			end
		elseif random == 4 then
			player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Text = "Blue"
			player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.TextColor3 = Color3.fromRGB(0, 0, 255)
			local t = ts:Create(player.PlayerGui:WaitForChild("ScreenGui").ReadyUp, ti, {TextTransparency = 1})
			t:Play()
			t.Completed:Connect(function()
				player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Visible = false
			end)
			task.wait(waitTime)
			for i, v in pairs(workspace.Map:GetChildren()) do
				if v.Name ~= player.PlayerGui.ScreenGui.ReadyUp.Text and v:IsA("Part") then
					v.Transparency = 0.7
					v.CanCollide = false
				end
			end
			task.wait(math.sqrt(workspace.Gravity / player.Character.Torso.Position.Y))
			if player.Character.Humanoid.Health ~= 0 then
				if player.MembershipType == Enum.MembershipType.None then
					player.leaderstats.Coins.Value += 50
				elseif player.MembershipType == Enum.MembershipType.Premium then
					player.leaderstats.Coins.Value += 75
				end
			else
				if player.MembershipType == Enum.MembershipType.None then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 50 / 2
					end
				elseif player.MembershipType == Enum.MembershipType.Premium then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 75 / 5
					end
				end
			end
		end
		task.wait(3)
		player:LoadCharacter()
		for i, v in pairs(workspace.Map:GetChildren()) do
			if v.Name ~= player.PlayerGui.ScreenGui.ReadyUp.Text and v:IsA("Part") then
				v.Transparency = 0
				v.CanCollide = true
			end
		end
		round()
	end
	task.wait(3.5)
	round()
end)

you can do a loop as shown here :

local Starting = false
local function round()
while wait(1) do
if #game.Players:GetPlayers() >= 2 then
if Starting == false then
Starting = true
-- stuff
end
end
end

Yes it will, technically you can use a remote event.

With a remote event you will have to find a player to target no?

1 Like

Well, using a local script, calling FireServer() will automatically give you the local player

1 Like

do you want it to start if there is enough players?

I believe he needs the player for his script, however, using PlayerAdded will start a new round Everytime someone joins

2 Likes

i need to update the gui for every player without having to stop the round process.

1 Like

you want to update a “text”?
[Char Limits]

1 Like

it doesn’t really matter, it should start when a player joins but if another person joins too, it should be on the same page as player 1.

yes, thats what i want.
[Char Limits]

you could create a StringValue and parent it to ReplicatedStorage.
and in that script
replace these lines

player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Text = "Red"

by

local Status = game:GetService("ReplicatedStorage").Status -- Add This Above
Status.Value = "Red"

create a localscript and parent it to the ReadyUp

local Status = game:GetSerivce("ReplicatedStorage").Status
local ReadyUp = script.Parent
Status.Changed:Connect(function(val)
ReadyUp.Text = val
end)


this is how the game is looking however using game.Players.PlayerAdded:Connect wouldn’t be ideal for multiplayer. I want to edit my current script to make it so that no issues like that pop up when two or more players are in game

you shouldn’t edit PlayerGui’s in a serverscript.

1 Like

idea: use FireAllClients to edit the gui for everyone. Thoughts?

1 Like

you can do that but why firing all clients multiplie times? in this case in recommend you to do what i did below.

ur not really firing it multiple times, you’ll do it once for every client.

Well, what you can do is doing those things in a local script and setting up the player like game.Players.localplayer.

All the guis for the player inself would be in that local script and then you can use remote events for the rest of the things.

i did remote events for guis. how can i find player for the stuff like incrementing the coin value?

here

local player = game.Players.LocalPlayer

game.ReplicatedStorage.YourEvent.OnClientEvent:Connect(function()
TextLabel.Text = player.leaderstats.Coins.Value
end)