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
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.
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(
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)
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
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
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
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
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)