You must change this to the location of your seat, leaving it nil will cause the script to error.
Something like this?
Yes, but:
Tell me if you need help, and you may put your script in ServerScriptService
Oh, Very sorry didn’t see that my bad… So the ServerScript goes to StarterPlayerScripts
and the LocalScript goes to ServerScriptService
Am I correct?
Edit: Sorry if I am asking to many questions. Just making sure I’m doing it right.
Actually the opposite, and I had an error in my script, it should be fixed now:
-- 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)
Re-paste the script into their respective classes accordingly. If this works for you, please mark this as a solution so other people that need help with this topic will know in the future.
Very sorry to tell you this because you probably worked on the script hard but it sadly doesnt work at all…
Do you get any errors? Please send me a video of what happens. I cannot test it myself as I do not have access to a mobile device.
Wait nevermind it seems to work in test studio but in-game it acts strange let me test this again…
Okay, For some reason this thing works in Studio but not in-game?? Very confused…
Studio Test:
In-game Test:
I think I may know the problem, try changing your code to this:
if Player then
if Player:GetAttribute("CanPlayPiano") then
-- run your code here
else
print(string.format("%s is on mobile and cannot play the piano.", Player.DisplayName))
end
end
It might have been since before I needed the player to exist and have the boolean to be true.
Sorry, What and where do I change the code exactly?
Change this section to the code I mentioned.
You sure you just changed that section I mentioned? Please hover over it and tell me what it says. Your server script should look like this:
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 then
if Player:GetAttribute("CanPlayPiano") then
-- run your code here
else
print(string.format("%s is on mobile and cannot play the piano.", Player.DisplayName))
end
end
end)
Okay fixed the error and yeah it looks exactly like that. Imma test if it works in-game now
Sadly, The same thing happenend again. In the Studio it works, In-Game it doesn’t? It’s honestly kinda suprising why this doesn’t work.
I have no idea why it wouldn’t work. Try this new code in the LocalScript:
-- 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 then
CanPlayPianoEvent:FireServer(true)
else
CanPlayPianoEvent:FireServer(false)
end
Other than that, I am completely out of ideas. Make sure to check the output too, you can do this by doing /console
in chat.
Try changing your code to this:
local CanPlayPianoEvent: RemoteEvent = ReplicatedStorage:WaitForChild("CanPlayPianoEvent")
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 then
if Player:GetAttribute("CanPlayPiano") then
-- run your code here
else
print(string.format("%s is on mobile and cannot play the piano.", Player.DisplayName))
end
end
end)
And then, make a new RemoteEvent
called CanPlayPianoEvent
and place it inside of ReplicatedStorage
. It also looks like you forgot to add a t
at the end of your seat by looking at the errors.
I can certainly try to help you with this problem! It sounds like you want to prevent users who are using mobile devices from interacting with certain elements in your game. Here is one way you could achieve this:
- Use the
UserInputService
to detect whether the user is using a mobile device or not. You can do this by checking theTouchEnabled
property, as you have already done in your code. - If the user is using a mobile device, you can use the
HumanoidRootPart
of their character to move them out of the seat or away from the piano. You can do this by setting theCFrame
property of theHumanoidRootPart
to a new position. - If the user is using a computer, you can allow them to interact with the seat or piano as normal.
Here is some example code that demonstrates how you could implement these steps:
local UserInputService = game:GetService("UserInputService")
local function onCharacterAdded(character)
-- Check if the user is using a mobile device
if UserInputService.TouchEnabled and not UserInputService.KeyboardEnabled and not UserInputService.MouseEnabled then
-- Move the character out of the seat or away from the piano
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
humanoidRootPart.CFrame = humanoidRootPart.CFrame + Vector3.new(0, 0, 5)
end
end
-- Connect the CharacterAdded event to the onCharacterAdded function
game.Workspace.ChildAdded:Connect(onCharacterAdded)
This code will detect when a new character is added to the workspace, and if the user is using a mobile device, it will move the character out of the seat or away from the piano.
I hope this helps! Let me know if you have any questions or if you need further assistance.