Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

If you press t, it will make the player sit, but it doesn't work?

Asked by 2 years ago
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)

2 answers

Log in to vote
1
Answered by 2 years ago

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)
0
that repeat is incredibly useless in this case where you use an event. this script will work and the reason that the original posters code didnt work is because he had characteradded in it greatneil80 2647 — 2y
0
This is a very bad practice because when your character falls into the void, it doesn't work since player character is removed, it's directly indexed and will send an error because it does not exist Xapelize 2658 — 2y
0
by the way this is a localscript, but if you make yourself sit in localscript only you can see Xapelize 2658 — 2y
0
also theres no way to make a localscript run before player spawning, but incase, you should use playeradded event, it's still not wrong (edit: im unsure of this one) Xapelize 2658 — 2y
View all comments (2 more)
0
i upvoted your post, keep on scripting, i believe in you Xapelize 2658 — 2y
0
i saw that he was using a local script so i just followed the same. if they REALLY wanted it to show for everyone they are free to reply to the post. I just wanted to improve thier script. Didnt want to spoon feed the poster with a whole new advanced script, thats not a good way to learn. Also localscripts run automatically at the start of the game. if you are really bothered i will fix it MarTieMak 56 — 2y
Ad
Log in to vote
0
Answered by
amanda 1059 Moderation Voter
2 years ago

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:

Intro to Player Input

UserInputService (API Reference)

Answer this question