How to make a PC User only seat

Here is a more detailed explanation of the code I provided above:

  1. First, we get a reference to the UserInputService using game:GetService("UserInputService") . This service provides information about the user’s input devices (e.g. keyboard, mouse, touch screen).
  2. Next, we define a function called onCharacterAdded which will be called whenever a new character is added to the workspace. This function takes one parameter, character , which is the character that was added.
  3. Inside the onCharacterAdded function, we use the TouchEnabled property of the UserInputService to check whether the user is using a mobile device. If the user is using a mobile device (i.e. TouchEnabled is true ), we want to move the character out of the seat or away from the piano.
  4. To move the character, we first get a reference to the HumanoidRootPart of the character using character:WaitForChild("HumanoidRootPart") . The HumanoidRootPart is the root part of the character’s humanoid model, and it determines the position of the character in the game world.
  5. Next, we set the CFrame property of the HumanoidRootPart to a new position. The CFrame property is a coordinate frame that represents the position and orientation of an object in 3D space. By setting the CFrame to a new value, we can move the object to a new location. In this case, we are moving the character away from the seat or piano by 5 units along the Z-axis (i.e. along the depth axis).
  6. Finally, we connect the CharacterAdded event to the onCharacterAdded function using game.Workspace.ChildAdded:Connect(onCharacterAdded) . This ensures that the onCharacterAdded function will be called whenever a new character is added to the workspace.

I hope this helps to clarify the code! Let me know if you have any questions or if you need further assistance.

something like this?
image

Yes, and make sure you created your RemoteEvent as said previously.

Yes, I did create it.
image

Alright, your code should work then. Let me know if it doesn’t, and please check in a live game first.

Doesn’t Work… (sad)

It looks like there are some issues with your code. Here are some suggestions to fix it:

  1. Make sure you have the correct syntax for all of your variables. For example, it looks like you’re missing a quotation mark at the end of each string that you’re passing to game:GetService() .
  2. Make sure you have the correct object names. For example, it looks like you’re trying to access an object called Piano , one , Bench , and Seat , but it’s not clear if those objects exist in your game.
  3. Make sure you have the correct capitalization for all of your variables and object names. For example, Seat and Humanoid should have a capital letter at the beginning.
  4. Make sure you have the correct spelling for all of your variables and object names. For example, it looks like you spelled Humanoid as Humnoid and CanPlayPianoEvent as CanPLayPianoEvent .
  5. Make sure you have the correct syntax for your if statement. It looks like you’re missing the then keyword after the if condition.
  6. Make sure you have the correct syntax for your string.format() function. It looks like you’re missing a quotation mark at the end of the string.

I hope these suggestions help! Let me know if you have any other questions.

Could you test it in a game where your alone? It looks like you’re friends are triggering a bunch of errors.

I will test it tommorow it is currently 2 am going to sleep now cya.

edit: They aren’t my friends just random people playing the game.

2 Likes

Did you try disabling the seat so you can’t touch it to sit in it because the script doesn’t prevent sitting in it it.

It broke the whole gui for it…?

pc:


mobile:

I think I found a way that also worked for me, and it involves a disabled seat:

So first, create a Script and put it in the seat that has the Disabled property set to true, so nobody can sit in it. Put this code in the script:

local debounce = false

script.Parent.Touched:Connect(function(hit)
	if not debounce then
		debounce = true
		if not script.Parent.Occupant then
           game.ReplicatedStorage.PlayerOnMobile:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
        end

	end
	wait(1)
	debounce = false
end)

game.ReplicatedStorage.PlayerOnMobile.OnServerEvent:Connect(function(player, result)
	if result == false then
		script.Parent:Sit(player.Character:FindFirstChildWhichIsA("Humanoid"))
	else
		wait()
	end
end)

Next, create a RemoteEvent in ReplicatedStorage, and call PlayerOnMobile. Then, create a LocalScript in StarterPlayer > StarterPlayerScripts. Put in the following code:

game.ReplicatedStorage.PlayerOnMobile.OnClientEvent:Connect(function()
	if game.UserInputService.TouchEnabled == true and game.UserInputService.KeyboardEnabled == false and game.UserInputService.MouseEnabled == false then
		game.ReplicatedStorage.PlayerOnMobile:FireServer(true)
	elseif game.UserInputService.TouchEnabled == false and game.UserInputService.KeyboardEnabled == true and game.UserInputService.MouseEnabled == true then
		game.ReplicatedStorage.PlayerOnMobile:FireServer(false)
	end
end)

Here’s how it works: The seat is disabled which means nobody can sit in it, regardless of which device they’re playing in, however, Seat:Sit() can still put players in the seat. When a player touches the seat, the BasePart.Touched event fires.

When this happens, the Debounce variable is set to true so we don’t fire the event more than once. Then, it checks if there is no occupant, and if there’s none then we fire the remote event named PlayerOnMobile. Then after a second, the Debounce variable is set to false.

When the LocalScript receives that event, it checks the TouchEnabled, MouseEnabled, and KeyboardEnabled properties on UserInputService. Then it fires the RemoteEvent again with an argument containing a true or false value. Back on the Seat Script, once it has received that event, it checks the result argument. If it’s true (Meaning the player is on mobile), then it won’t do anything. But if they are on PC (If it is false), then Seat:Sit() is called, which places the character onto the seat.

Also is the script to open the GUI separate? (Which I think it is)

1 Like

–Code here

I think this is what you are looking for:
local UserInputService = game:GetService(“UserInputService”)
if UserInputService.TouchEnabled and not UserInputService.KeyboardEnabled and not UserInputService.MouseEnabled then
return
elseif not UserInputService.TouchEnabled and UserInputService.KeyboardEnabled and UserInputService.MouseEnabled then
local player = game.Players.LocalPlayer
local char = player.Character
local plrGui = player:WaitForChild(“PlayerGui”)
local panel = plrGui:WaitForChild(“Panel”)
local canv = panel:WaitForChild(“Canvas”)
local frame = canv:WaitForChild(“Frame”)
local button = frame:WaitForChild(“Button”)
button.Text = “Sit”
button.Position = UDim2.new(0.16, 0, 0.14, 0)
button.Size = UDim2.new(0.16, 0, 0.13, 0)
button.Parent = canv
button.MouseButton1Down:Connect(function()
local part = workspace.Seat.Part
local hum = char:WaitForChild(“Humanoid”)
char:MoveTo(part.Position + Vector3.new(0, 4, 0))
hum:MoveTo(part.Position + Vector3.new(0, 4, 0))
hum.Sit = true
end)
end