How to get PlayerScripts on server side?

Hello developers , I want to make script that gives player LocalScript.

The problem is that PlayerScripts exists on client side in game.Players.(Player Name Here).
How can I give player LocalScript by script on server side?

Thanks :slightly_smiling_face:

3 Likes

Here’s an example to give a LocalScript to a specific player when they join the game:

local localScript = -- Where you LocalScript is stored

game:GetService("Players").PlayerAdded:Connect(function(player)
	if player.UserId == 123456 then -- Change this to the userId of the player you want to give the LocalScript to
		localScript.Parent = player.PlayerScripts
	end
end)

Edit: This is a server script inside ServerScriptService by the way, I forgot to mention that

3 Likes

This is not what I am looking for,
player gets the LocalScript when sits on seat. I already have script for it, but it isn’t working since PlayerScripts exists on client side

2 Likes

the .PlayerScripts folder isn’t replicated on the server and only that client can see their own PlayerScripts folder.

I’m guessing you mean parenting a local script inside the PlayerScripts folder in the player object, you can’t do this but you can place local scripts in StarterPlayer.StarterPlayerScripts where all its descendants are cloned into player.PlayerScripts.

If you want to add a local script when you want, you’re going to need a work around

task.wait()
script.Parent = game:GetService('Players').LocalPlayer:WaitForChild('PlayerScripts')

at the top of the said local script.

2 Likes

You didn’t mention you want to give a player a LocalScript when they sit in a seat

If you want to give players control of a vehicle for example then the best way to do this is by following these steps:

  1. Make a LocalScript inside StarterCharacterScripts
  2. Inside the LocalScript write:
script.Parent:WaitForChild("Humanoid").Seated:Connect(function(active, seat)
	if active then
		-- Control the vehicle inside here
	else
		-- Remove the controls for the vehicle inside here
	end
end)

@legs_v I honestly wasn’t aware that PlayerScripts isn’t accessible from the server (I assumed it was since PlayerGui is replicated), thanks for pointing that out

Edit: @kapeczun Here’s an example of what I mean by Control the vehicle inside here:

local throttleConnection

script.Parent:WaitForChild("Humanoid").Seated:Connect(function(active, seat)
	if active then
		if seat.Parent.Name == "NameOfYourVehicleModel" then

			throttleConnection = seat:GetPropertyChangedSignal("Throttle"):Connect(function()
				
			end)

		end
	else
		if throttleConnection then
			throttleConnection:Disconnect()
			throttleConnection = nil
		end
	end
end)
1 Like

Yes, I mean parenting LocalScript into PlayerScripts. I forgot to mention (sorry about that) that player only should have this LocalScript only when sitting on seat.

1 Like

Sorry I didn’t mention that.

I also don’t know how I could use your script.
I already have that LocalScript prepared for cloning inside script. I’ll place a screenshoot and give an explanation.

image
GUIOnSeat - Gives and removes SeatGui and PoleCamera if player sits or jumps off Seat
SeatGui - Just a Gui. Everything works, no help neded here.
PoleCamera - That problematic LocalScript which should go into PlayerScripts

Edit: GUIOnSeat only works for giving SeatGui, it’s not working for PoleCamera

You can just copy it to the player’s PlayerGui and it will run fine, its just a bit uneedingly difficult to copy it to a folder that also isn’t on the server like PlayerScripts.

What’s the name of the seat’s parent model?

The name of seat’s parent model is “Tram”.

Oh wait it’s working,
Parenting the LocalScript in PlayerGui works.
image

Thank you for your help :+1:

1 Like

JohhnyLegoKing thanks for your support also. The solution from dandcx worked for me

Thank you too :+1:

1 Like

I’m gonna show you what I was making anyways. If you want to give the LocalScript only once you can do:

local character = script.Parent
local playerScripts = game:GetService("Players"):GetPlayerFromCharacter(character):WaitForChild("PlayerScripts")

local poleCamera = -- Where your PoleCamera script is

character:WaitForChild("Humanoid").Seated:Connect(function(active, seat)
	if active and seat.Parent.Name == "Tram" then
		poleCamera.Parent = playerScripts
	end
end)

but if you wanted to take it away when they stand up you’ll need to do:

local character = script.Parent
local playerScripts = game:GetService("Players"):GetPlayerFromCharacter(character):WaitForChild("PlayerScripts")

local poleCamera = -- Where your PoleCamera script is
local poleCameraParent = poleCamera.Parent

character:WaitForChild("Humanoid").Seated:Connect(function(active, seat)
	if active and seat.Parent.Name == "Tram" then
		poleCamera.Parent = playerScripts
	else
		poleCamera.Parent = poleCameraParent
	end
end)

This is a LocalScript inside of StarterCharacterScripts by the way

2 Likes

This also seems like a good solution,
Thank you for your help

3 Likes

you will mark him as solution and not your self as the solution since he gave you the solution

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.