local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.KeyDown:connect(function(key) if key:lower() == "t" or key:upper() == "T" then game.Players.PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(Character) Character.Humanoid.Sit = true end) end) end end)
I tested out your script and it also did not work for me. I don't know exactly how your code did not work but I can help you out a bit.
First of all instead of putting 'game.Players.PlayerAdded:Connect(function(Player))' Its better to wait in your script until the player loads. Something like this would work
local player = game.Players.LocalPlayer repeat wait() until player.Character print('char loaded')
Also, I do not suggest using mouse.KeyDown. I would use UserInputService like this
local UserInputService = game:GetService("UserInputService") UserInputService.InputBegan:Connect(function(input) -- Checking if user presses a key if input.KeyCode == Enum.KeyCode.T then -- Checking if the key that the user pressed was T (lowercase or uppercase) -- do stuff end end)
Now if you combine those two things you could make a script that works like this!
local UserInputService = game:GetService("UserInputService") local player = game.Players.LocalPlayer repeat wait() until player.Character UserInputService.InputBegan:Connect(function(input) -- Checking if user presses a key if input.KeyCode == Enum.KeyCode.T then -- Checking if the key that the user pressed was T (lowercase or uppercase) player.Character.Humanoid.Sit = true -- Make the player sit end end)
Firstly, the logic is wrong. You do not need to wait for players nor characters to load, you just need to check if the character exists at the time you are pressing T, and otherwise do nothing.
local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.KeyDown:Connect(function(key) if key:lower() == "t" and player.Character then player.Character.Humanoid.Sit = true end end)
Secondly, KeyDown
is a deprecated method. While the code above may work for the time being, it can potentially be removed in a future update. For this reason, it is best that you learn it's replacement: InputBegan
.
local player = game.Players.LocalPlayer local UIS = game:GetService("UserInputService") UIS.InputBegan:Connect(function(input) if input.KeyCode.Name == "T" and player.Character then player.Character.Humanoid.Sit = true end end)
Here are some links so that you can learn more about input handling: