The above are super inefficient. I recommend using my code as seen below:
-- LocalScript
local UserInputService = game:GetService("UserInputService")
local PlayerService = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CanPlayPianoEvent: RemoteEvent = ReplicatedStorage:WaitForChild("CanPlayPianoEvent")
if UserInputService.MouseEnabled and UserInputService.KeyboardEnabled and not UserInputService.TouchEnabled then
CanPlayPianoEvent:FireServer(true)
else
CanPlayPianoEvent:FireServer(false)
end
-- ServerScript
local PlayerService = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Seat: Seat = nil
local CanPlayPianoEvent = Instance.new("RemoteEvent")
CanPlayPianoEvent.Name = "CanPlayPianoEvent"
CanPlayPianoEvent.Parent = ReplicatedStorage
CanPlayPianoEvent.OnServerEvent:Connect(function(player, isDesktop)
player:SetAttribute("CanPlayPiano", isDesktop)
end)
Seat:GetPropertyChangedSignal("Occupant"):Connect(function(humanoid: Humanoid)
local Character = humanoid.Parent
local Player = PlayerService:GetPlayerFromCharacter(Character)
if Player and Player:GetAttribute("CanPlayPiano") then
-- play the piano and such
else
print("Player cannot play piano as they don't have a keyboard and mouse!")
end
end)
Basically, we have a local script that checks if the player is playing on a desktop, and if so, we fire an event with a boolean. On the server script, we listen to that event and receive the boolean. We then create an attribute on the player called CanPlayPiano
which indicates if the player can play the piano or not. After this, we listen to the Occupant
property changing on the seat. When it changes, we get the player who sat in the seat, and then we check if the player exists and the CanPlayPiano
attribute is true. If the conditions are met, you can run your code that makes you play the piano. If both of the conditions are not met, then we will print that the user is not playing on the desktop and therefore cannot play the piano.
Your server script should be inside the part, and your local script should be inside StarterPlayerScripts
. In your server script, change Seat
to the path of your part.