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

Acces LocalPlayer by a ServerScriptService script?

Hello! I'm trying to make a round system for my wars game. For that, I need to acces LocalPlayer by a normal script in ServerScriptService. the script is the following:

wait(20)
local PlayerService = game:GetService("Players")
local Player = PlayerService:GetPlayers()
local function toround()
	game.Workspace.Rift.Rift.Rift.TimePosition = 9.5
	game.Workspace.Rift.Rift.Rift:Play()
	game.Workspace.Rift.Rift.Transparency = 0
	Player.Character.HumanoidRootPart.CFrame = game.Workspace.Rift.Rift.CFrame
	wait(5)
	game.Workspace.Rift.Rift.Transparency = 1
end
local function tolobby()
	Player.Backpack:ClearAllChildren()
	Player.Character.Humanoid.Health = 100
	game.Workspace.Lobby.LobbyRift.Rift.Rift.TimePosition = 9.5
	game.Workspace.Lobby.LobbyRift.Rift.Rift:Play()
	game.Workspace.Lobby.LobbyRift.Rift.Transparency = 0
	Player.Character.HumanoidRootPart.CFrame = game.Workspace.Lobby.LobbyRift.Rift.CFrame
	wait(5)
	game.Workspace.Lobby.LobbyRift.Rift.Transparency = 1
	game.Workspace.Drop.Drop.ReEnable.Disabled = true
	game.Workspace.Drop.PositionRift.Transparency = 1
	game.Workspace.Drop.Laser.Transparency = 1
	game.Workspace.Drop.Parachute.Transparency = 1
	game.Workspace.Drop.Drop.Transparency = 1
	game.Workspace.Drop.Drop.Move.Disabled = true
	game.Workspace.Drop.Drop.System.Disabled = true
	game.Workspace.Drop.Drop.Reset.Disabled = true
	game.Workspace.Drop.Drop.ReEnable.Disabled = true
	game.Workspace.Drop.Drop.BillboardGui.Enabled = false
end
local function startdrop()
	game.Workspace.Drop.Drop.ReEnable.Disabled = false
end
local Value = script.Value.Value
local Gamemodes = script.Parent.Gamemodes
--Start cycle
tolobby()
while true do
	Value = math.random(1, 9)
	if Value == 1 then
		wait(10)
		Gamemodes.OnlyARsMode["[NON VIP] Assault rifle"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlyARsMode["[NON VIP] Burst assault rifle"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlyARsMode["[NON VIP] Med kit"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlyARsMode["[NON VIP] Parachute"]:Clone().Parent = Player
		toround()
		wait(20)
		Value = 0
		return tolobby()
	end
	if Value == 2 then
		wait(10)
		Gamemodes.OnlySMGsMode["[NON VIP] Burst submachine gun"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlySMGsMode["[NON VIP] Fast fire submachine gun"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlySMGsMode["[NON VIP] Submachine gun"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlySMGsMode["[NON VIP] Suppresed submachine gun"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlySMGsMode["[NON VIP] Med kit"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlySMGsMode["[NON VIP] Parachute"]:Clone().Parent = Player
		toround()
		wait(20)
		Value = 0
		return tolobby()
	end
	if Value == 3 then
		wait(10)
		Gamemodes.OnlyShotgunsMode["[NON VIP] Pump shotgun"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlyShotgunsMode["[NON VIP] Tactical shotgun"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlyShotgunsMode["[NON VIP] Med kit"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlyShotgunsMode["[NON VIP] Parachute"]:Clone().Parent = Player
		toround()
		wait(20)
		Value = 0
		return tolobby()
	end
	if Value == 4 then
		wait(10)
		Gamemodes.OnlySNRsMode["[NON VIP] Sniper rifle"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlySNRsMode["[NON VIP] Med kit"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlySNRsMode["[NON VIP] Parachute"]:Clone().Parent = Player
		toround()
		wait(20)
		Value = 0
		return tolobby()
	end
	if Value == 5 then
		wait(10)
		startdrop()
		Gamemodes.OnlyDropsMode["[NON VIP] Parachute"]:Clone().Parent = Player.Backpack
		toround()
		wait(20)
		Value = 0
		return tolobby()
	end
	if Value == 6 then
		wait(10)
		Gamemodes.OnlyPistolsMode["[NON VIP] Pistol"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlyPistolsMode["[NON VIP] Suppresed pistol"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlyPistolsMode["[NON VIP] Med kit"]:Clone().Parent = Player.Backpack
		Gamemodes.OnlyPistolsMode["[NON VIP] Parachute"]:Clone().Parent = Player.Backpack
		toround()
		wait(20)
		Value = 0
		return tolobby()
	end
	if Value == 7 or 8 or 9 then
		wait(10)
		startdrop()
		Gamemodes.ClassicMode["[NON VIP] Assault rifle"]:Clone().Parent = Player.Backpack
		Gamemodes.ClassicMode["[NON VIP] Burst assault rifle"]:Clone().Parent = Player.Backpack
		Gamemodes.ClassicMode["[NON VIP] Pump shotgun"]:Clone().Parent = Player.Backpack
		Gamemodes.ClassicMode["[NON VIP] Tactical shotgun"]:Clone().Parent = Player.Backpack
		Gamemodes.ClassicMode["[NON VIP] Submachine gun"]:Clone().Parent = Player.Backpack
		Gamemodes.ClassicMode["[NON VIP] Med kit"]:Clone().Parent = Player.Backpack
		Gamemodes.ClassicMode["[NON VIP] Parachute"]:Clone().Parent = Player
		toround()
		wait(20)
		Value = 0
		return tolobby()
	end
	wait()
end

Is there any way to acces LocalPlayer by a normal script in the ServerScriptService?

3 comments

0

It is not possible to do this from a server script without the use of remote bindables.

0

No

0

Could you show us what events are going to trigger these function to occur? If so, I may be able to change up the script so that a remote event can pass through and fix your issue.

2 answers

✓ Accepted answer

In short, yes you can access any player on the server, just not by using LocalPlayer, instead you'd need to either use the :GetPlayers() or PlayerAdded method of the Players Service. You can also use RemoteEvents and RemoteFunctions, but based on your script those aren't required

Improvements

Use :WaitForChild() to make sure an instance exists before using it

Use local variables for instances that are used multiple times throughout your code

Use elseif and else instead of multiple if statements

The wait(10) and wait(20) in your if statements are only needed once and should be moved outside of the conditional to improve readability and use up less lines

Issues

Line 36 only scripts and folders should be inside the ServerScriptService, I suggest moving the tools to a folder in ServerStorage instead

Line 40 rewrites the definition of Value on Line 35 instead of setting the value as the returned integer (technically not necessary for the loop)

Line 106 you only have a comparison for "Value == 7," the or statement used doesn't have a comparison so you are essentially only checking if 8 and 9 are not nil

Line 119 tolobby() doesn't return any values

Revised Server Script

local Workspace = game:GetService("Workspace")
local PlayerService = game:GetService("Players")

local WorkspaceRift = Workspace:WaitForChild("Rift"):WaitForChild("Rift")
local WorkspaceRiftAudio = WorkspaceRift:WaitForChild("Rift")
local LobbyRift = Workspace:WaitForChild("Lobby"):WaitForChild("LobbyRift"):WaitForChild("Rift")
local LobbyRiftAudio = LobbyRift:WaitForChild("Rift")
local WorkspaceDrop = Workspace:WaitForChild("Drop")
local DropChild = WorkspaceDrop:WaitForChild("Drop")

local function ToRound()
	for index, Player in pairs(PlayerService:GetPlayers()) do
		Player.Character:WaitForChild("HumanoidRootPart").CFrame = WorkspaceRift.CFrame
	end
	WorkspaceRiftAudio.TimePosition = 9.5
	WorkspaceRiftAudio:Play()
	WorkspaceRift.Transparency = 0
	wait(5)
	WorkspaceRift.Transparency = 1
end

local function ToLobby()
	for index, Player in pairs(PlayerService:GetPlayers()) do
		Player:WaitForChild("Backpack"):ClearAllChildren()
		Player.Character:WaitForChild("Humanoid").Health = 100
		Player.Character:WaitForChild("HumanoidRootPart").CFrame = LobbyRift.CFrame
	end
	
	LobbyRiftAudio.TimePosition = 9.5
	LobbyRiftAudio:Play()
	LobbyRift.Transparency = 0
	wait(5)
	LobbyRift.Transparency = 1
	WorkspaceDrop:WaitForChild("PositionRift").Transparency = 1
	WorkspaceDrop:WaitForChild("Laser").Transparency = 1
	WorkspaceDrop:WaitForChild("Parachute").Transparency = 1
	DropChild.Transparency = 1
	DropChild:WaitForChild("Move").Disabled = true
	DropChild:WaitForChild("System").Disabled = true
	DropChild:WaitForChild("Reset").Disabled = true
	DropChild:WaitForChild("ReEnable").Disabled = true
	DropChild:WaitForChild("BillboardGui").Enabled = false
end

local function StartDrop()
	DropChild:WaitForChild("ReEnable").Disabled = false
end

local Val = script:WaitForChild("Value")
local Gamemodes = script.Parent:WaitForChild("Gamemodes")
local AR = Gamemodes:WaitForChild("OnlyARsMode")
local SMG = Gamemodes:WaitForChild("OnlySMGsMode")
local Shotgun = Gamemodes:WaitForChild("OnlyShotgunsMode")
local SNR = Gamemodes:WaitForChild("OnlySNRsMode")
local Drop = Gamemodes:WaitForChild("OnlyDropsMode")
local Pistol = Gamemodes:WaitForChild("OnlyPistolsMode")
local Classic = Gamemodes:WaitForChild("ClassicMode")

-- Start Cycle
while true do
	wait(20)
	Val.Value = 0
	ToLobby()
	Val.Value = math.random(1, 9)
	wait(10)
	
	for index, Player in pairs(PlayerService:GetPlayers()) do
		local Backpack = Player:WaitForChild("Backpack")
		if Val.Value == 1 then
			AR:WaitForChild("[NON VIP] Assault rifle"):Clone().Parent = Backpack
			AR:WaitForChild("[NON VIP] Burst assault rifle"):Clone().Parent = Backpack
			AR:WaitForChild("[NON VIP] Med kit"):Clone().Parent = Backpack
			AR:WaitForChild("[NON VIP] Parachute"):Clone().Parent = Player
		elseif Val.Value == 2 then
			SMG:WaitForChild("[NON VIP] Burst submachine gun"):Clone().Parent = Backpack
			SMG:WaitForChild("[NON VIP] Fast fire submachine gun"):Clone().Parent = Backpack
			SMG:WaitForChild("[NON VIP] Submachine gun"):Clone().Parent = Backpack
			SMG:WaitForChild("[NON VIP] Suppresed submachine gun"):Clone().Parent = Backpack
			SMG:WaitForChild("[NON VIP] Med kit"):Clone().Parent = Backpack
			SMG:WaitForChild("[NON VIP] Parachute"):Clone().Parent = Player
		elseif Val.Value == 3 then
			Shotgun:WaitForChild("[NON VIP] Pump shotgun"):Clone().Parent = Backpack
			Shotgun:WaitForChild("[NON VIP] Tactical shotgun"):Clone().Parent = Backpack
			Shotgun:WaitForChild("[NON VIP] Med kit"):Clone().Parent = Backpack
			Shotgun:WaitForChild("[NON VIP] Parachute"):Clone().Parent = Player
		elseif Val.Value == 4 then
			SNR:WaitForChild("[NON VIP] Sniper rifle"):Clone().Parent = Backpack
			SNR:WaitForChild("[NON VIP] Med kit"):Clone().Parent = Backpack
			SNR:WaitForChild("[NON VIP] Parachute"):Clone().Parent = Player
		elseif Val.Value == 5 then
			Drop:WaitForChild("[NON VIP] Parachute"):Clone().Parent = Backpack
		elseif Val.Value == 6 then
			Pistol:WaitForChild("[NON VIP] Pistol"):Clone().Parent = Backpack
			Pistol:WaitForChild("[NON VIP] Suppresed pistol"):Clone().Parent = Backpack
			Pistol:WaitForChild("[NON VIP] Med kit"):Clone().Parent = Backpack
			Pistol:WaitForChild("[NON VIP] Parachute"):Clone().Parent = Backpack
		else
			Classic:WaitForChild("[NON VIP] Assault rifle"):Clone().Parent = Backpack
			Classic:WaitForChild("[NON VIP] Burst assault rifle"):Clone().Parent = Backpack
			Classic:WaitForChild("[NON VIP] Pump shotgun"):Clone().Parent = Backpack
			Classic:WaitForChild("[NON VIP] Tactical shotgun"):Clone().Parent = Backpack
			Classic:WaitForChild("[NON VIP] Submachine gun"):Clone().Parent = Backpack
			Classic:WaitForChild("[NON VIP] Med kit"):Clone().Parent = Backpack
			Classic:WaitForChild("[NON VIP] Parachute"):Clone().Parent = Player
		end
	end
	
	if Val.Value == 5 or Val.Value > 6 then
		StartDrop()
	end
	
	ToRound()
end

Note, I iterate through the table returned by :GetPlayers() in order to make changes for all players in the game

There's no way to access a local player to a server sided event. The only other way is to use remote events

LocalPlayer is for local scripts only.

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