Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you get the local player in a server script?

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

6 comments

0

just dont use local player, and use the resources available to u. you've already done it so i dont rlly understand the issue. http://prntscr.com/mczyxp

0

The problem is it underlines where it says player when I use FireClient and has the warning of Unknown global 'player'

1

gioni the issue was at the end where he was sending a remote event to localplayer.

0

ok so u obviously havent defined player

0

ok good job dino man

0

yaya, i knew what was going on from a previous question. im not fixing the indention sorry its too many lines :(

1 answer

✓ Accepted answer

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


2 comments

0

Thank you SO MUCH I have been trying to get this script to work for so long and there have been errors after error but now it finally works!

0

Glad to help bud.

This discussion is preserved as it was. New answers, comments, and votes are closed.