How to call local player on server script?

I wrote a script (script excerpt) where when a player clicks on the UI button, he should sit on the seat, but I don’t know how to designate the plr variable, it should be the player who clicked on the button

game.StarterGui.ScreenGui.EnterButton.MouseButton1Click:Connect(function()
	local plr =
	if plr then
		local Seats = model.seats:GetChildren()
		for i, seat in pairs(Seats) do
			if not seat.Occupant then
				seat:Sit(plr.Character.Humanoid)
				plr.Character.Humanoid.JumpHeight = 0
				break
			end
		end
	end
end)

I tried to implement this with RemoteEvents, BindableEvents, _G but to no avail. Please help me and many thanks in advance for your help

1 Like

You can’t do “MouseButton1Click” on a server script. And also, the GUI is in the player (game.Players.USERNAME.PlayerGui) and not in StarterGui.

You need to use RemoteEvents for this.

Here is some examples:

LocalScript

script.Parent.MouseButton1Click:Connect(function()
      game:GetService("ReplicatedStorage").ExampleEvent:FireServer()
end)

ServerScript

game:GetService("ReplicatedStorage").ExampleEvent.OnServerEvent:Connect(function(player)
      print("player name is: "..player)
end)
2 Likes

This implementation is wrong. Let’s walk through how to do this:

First, remember that StarterGui merely clones its contents to the player when they join, so do NOT modify its contents because no one will see it. Each player has their UI container. You can get it by Player.PlayerGui.

Second, you have to use RemoteEvents for this. The correct way of implementing this would be
Button clicked > Fire event > Server gets the event, fired by player > Run code

Just go ahead and make an event in ReplicatedStorage, where accesible by both the server and clients.

With your previous code, we can now do this on a LocalScript:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = PATH_TO_REMOTE
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")

local EnterButton = PlayerGui.ScreenGui.EnterButton

EnterButton.Activated:Connect(function()
	RemoteEvent:FireServer()
end)

And this in a ServerScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = PATH_TO_REMOTE
local Seats = model.seats:GetChildren()

Remote.OnServerEvent:Connect(function(player)
	for _, seat in Seats do
		if not seat.Occupant then
			seat:Sit(player.Character.Humanoid)
				player.Character.Humanoid.JumpHeight = 0
			break
		end
	end
end)

OnServerEvent takes a player parameter by default, which is the player who fired it. With it you can have a reference of who fired it and do what actions with them.

3 Likes

Thank you so much for your help, thanks to you I not only found out the solution, but also learned more about roblox studio

1 Like

And thank you so much for your help too

1 Like

I just tested these scripts in my roblox studio and unfortunately they don’t work. I thought that the error is that the Seats are not in pairs() but it still does not work(

1 Like

What’s the error?​​​​​​​​​​​​​​​​​​​

1 Like

The player just doesn’t sit down

1 Like

Try using a local script. The player should still sit down server-side.

local plr = game:GetService("Players").LocalPlayer
plr.PlayerGui.ScreenGui.EnterButton.MouseButton1Click:Connect(function()
	if plr then
		local Seats = model.seats:GetChildren()
		for i, seat in pairs(Seats) do
			if not seat.Occupant then
				seat:Sit(plr.Character.Humanoid)
				plr.Character.Humanoid.JumpHeight = 0
				break
			end
		end
	end
end)
2 Likes

Thanks for your help, but unfortunately that didn’t help either

1 Like

for some reason seat:Sit() only works on a server script

1 Like

where is your script located at?

1 Like

you can use a RemoteEvent to send the player’s information to the server and you can listen for the RemoteEvent and handle the player’s information:

local remoteEvent = game.ReplicatedStorage.RemoteEvent

game.StarterGui.ScreenGui.EnterButton.MouseButton1Click:Connect(function()
    remoteEvent:FireServer()
end)
local remoteEvent = game.ReplicatedStorage.RemoteEvent

remoteEvent.OnServerEvent:Connect(function(player)
    local Seats = model.seats:GetChildren()
    for i, seat in pairs(Seats) do
        if not seat.Occupant then
            seat:Sit(player.Character.Humanoid)
            player.Character.Humanoid.JumpHeight = 0
            break
        end
    end
end)

Make sure to replace model with the actual reference to the model containing the seats in your game xx

2 Likes

It was in ServerScriptServise, I tried to port it to other places

1 Like

Thanks for your efforts, but I checked these scripts and they don’t work either, I don’t understand why. I guess I’ll just leave the whole script just in case

local remoteEvent = game.ReplicatedStorage.RemoteEvent

while true do
	
	local model = game.ServerStorage.pickuptrack:Clone()
	model.Parent = workspace
	
	for i = 0, 266 do
		model:PivotTo(model:GetPivot() + Vector3.new(3, 0, 0))
		wait()
	end

	remoteEvent.OnServerEvent:Connect(function(player)
		local Seats = model.seats:GetChildren()
		for i, seat in pairs(Seats) do
			if not seat.Occupant then
				seat:Sit(player.Character.Humanoid)
				player.Character.Humanoid.JumpHeight = 0
				break
			end
		end
	end)
	
	workspace.InvisibleWalls.TouchPart.CanTouch = true
	
	workspace.InvisibleWalls.TouchPart.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			game.ReplicatedStorage.EnterButtonFire:FireClient(player)
			
			workspace.InvisibleWalls.TouchPart.TouchEnded:Connect(function()
				game.ReplicatedStorage.EnterButtonHide:FireClient(player)
			end)
			
			wait(16)
			workspace.InvisibleWalls.TouchPart.CanTouch = false
			game.ReplicatedStorage.EnterButtonHide:FireClient(player)
		end
	end)
	
	for i = 15, 0, -1 do
		workspace.Waittext.SurfaceGui.TextBox.Text = 'Wait '..i..' seconds'
		wait(1)
	end
	
	workspace.Waittext.SurfaceGui.TextBox.Text = 'Wait...'
	
	for i = 0, 266 do
		model:PivotTo(model:GetPivot() + Vector3.new(3, 0, 0))
		wait()
	end
	
	model:Destroy()
	
end

Everything else works: the car pulls up, the button appears, the time is counted, the car drives off, the button disappears, and the player cannot sit down

1 Like

Ah! remoteEvent.OnServerEvent:Connect() function is not being called when the button is clicked! Try this boo

local remoteEvent = game.ReplicatedStorage.RemoteEvent

remoteEvent.OnServerEvent:Connect(function(player)
    local model = game.ServerStorage.pickuptrack:Clone()
    model.Parent = workspace

    for i = 0, 266 do
        model:PivotTo(model:GetPivot() + Vector3.new(3, 0, 0))
        wait()
    end

    local Seats = model.seats:GetChildren()
    for i, seat in pairs(Seats) do
        if not seat.Occupant then
            seat:Sit(player.Character.Humanoid)
            player.Character.Humanoid.JumpHeight = 0
            break
        end
    end

    workspace.InvisibleWalls.TouchPart.CanTouch = true

    workspace.InvisibleWalls.TouchPart.TouchEnded:Connect(function()
        game.ReplicatedStorage.EnterButtonHide:FireClient(player)
    end)

    wait(16)
    workspace.InvisibleWalls.TouchPart.CanTouch = false
    game.ReplicatedStorage.EnterButtonHide:FireClient(player)

    for i = 0, 266 do
        model:PivotTo(model:GetPivot() + Vector3.new(3, 0, 0))
        wait()
    end

    model:Destroy()
end)

workspace.InvisibleWalls.TouchPart.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        remoteEvent:FireServer(player)
    end
end)

while true do
    -- Rest of the code
end
1 Like

Unless after that all the code will be activated by pressing the button? I just wanted the player to just sit on one of the seats when the button is clicked

1 Like

Local scripts don’t work in ServerScriptService. Try putting it in the gui itself.

1 Like
local remoteEvent = game.ReplicatedStorage.RemoteEvent

remoteEvent.OnServerEvent:Connect(function(player)
    local model = game.ServerStorage.pickuptrack:Clone()
    model.Parent = workspace

    local Seats = model.seats:GetChildren()
    for i, seat in pairs(Seats) do
        if not seat.Occupant then
            seat:Sit(player.Character.Humanoid)
            player.Character.Humanoid.JumpHeight = 0
            break
        end
    end
end)

workspace.InvisibleWalls.TouchPart.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        remoteEvent:FireServer(player)
    end
end)
1 Like

I already tried, nothing has changed