Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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
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 Gey4Jesus69 2705 — 5y
0
The problem is it underlines where it says player when I use FireClient and has the warning of Unknown global 'player' puppybob123 8 — 5y
1
gioni the issue was at the end where he was sending a remote event to localplayer. DinozCreates 1070 — 5y
0
ok so u obviously havent defined player Gey4Jesus69 2705 — 5y
View all comments (2 more)
0
ok good job dino man Gey4Jesus69 2705 — 5y
0
yaya, i knew what was going on from a previous question. im not fixing the indention sorry its too many lines :( DinozCreates 1070 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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


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! puppybob123 8 — 5y
0
Glad to help bud. DinozCreates 1070 — 5y
Ad

Answer this question