I am trying to get the local player but I realized that it is on a non-local script. I am doing this:
local Players = game:GetService("Players") local player = Players.LocalPlayer
But I know that won't work. How should I do this?
Code for Script:
local oldMessage = "" local minPlayers = 1 local Players = game:GetService("Players") local player = Players.LocalPlayer local messagePlayerEvent = Instance.new("RemoteEvent") messagePlayerEvent.Parent = game.ReplicatedStorage messagePlayerEvent.Name = "MessagePlayerEvent" function teleportPlayers() local TeleportTarget = "" local target = CFrame.new(workspace.StartP1.Position) for i, player in ipairs(game.Players:GetChildren()) do TeleportTarget = (string.format("StartP%s", i)) target = CFrame.new(workspace[TeleportTarget].Position) player.Character.HumanoidRootPart.CFrame = target player.Playing.Value = 1 end end function message(message) if oldMessage ~= message then oldMessage = message print(message) end end function playersCurrentlyPlaying() for i, player in ipairs(game.Players:GetChildren()) do if player.Playing.Value == 1 then return true end end return false end game:GetService('Players').PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) character:WaitForChild("Humanoid").Died:Connect(function() player.Playing.Value = 0 end) end) local playing = Instance.new("IntValue", player) playing.Value = 0 playing.Name = "Playing" end) while(true) do wait(10) if #game.Players:getPlayers() >= minPlayers then if playersCurrentlyPlaying() then local text = "Waiting for an available arena..." messagePlayerEvent:FireClient(player, text) else local text = "There are enough players for a new game! Teleporting..." messagePlayerEvent:FireClient(player, text) wait(4) teleportPlayers() end else local text = "Waiting for more players..." messagePlayerEvent:FireClient(player, text) end end
Get necessary players to send message to, then use in pairs to send to all players.
local oldMessage = "" local minPlayers = 1 local messagePlayerEvent = Instance.new("RemoteEvent") messagePlayerEvent.Parent = game.ReplicatedStorage messagePlayerEvent.Name = "MessagePlayerEvent" function teleportPlayers() local TeleportTarget = "" local target = CFrame.new(workspace.StartP1.Position) for i, player in ipairs(game.Players:GetChildren()) do TeleportTarget = (string.format("StartP%s", i)) target = CFrame.new(workspace[TeleportTarget].Position) player.Character.HumanoidRootPart.CFrame = target player.Playing.Value = 1 end end function message(message) if oldMessage ~= message then oldMessage = message print(message) end end function playersCurrentlyPlaying() for i, player in ipairs(game.Players:GetChildren()) do if player.Playing.Value == 1 then return true end end return false end game:GetService('Players').PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) character:WaitForChild("Humanoid").Died:Connect(function() player.Playing.Value = 0 end) end) local playing = Instance.new("IntValue", player) playing.Value = 0 playing.Name = "Playing" end) while(true) do wait(10) if #game.Players:getPlayers() >= minPlayers then if playersCurrentlyPlaying() then local text = "Waiting for an available arena..." for i, player in ipairs(playersCurrentlyPlaying()) do messagePlayerEvent:FireClient(player, text) end else local text = "There are enough players for a new game! Teleporting..." for i, player in ipairs(game.Players:GetChildren()) do messagePlayerEvent:FireClient(player, text) end wait(4) teleportPlayers() end else local text = "Waiting for more players..." for i, player in ipairs(game.Players:GetChildren()) do messagePlayerEvent:FireClient(player, text) end end end