Help with if is in seat make able to run script

So I tried to do it but all my tries failed, here is the script I wanna is being runned if I sit on an seat :

Animation = Instance.new(“Animation”)
Animation.Parent = game.Workspace
Animation.AnimationId = “http://www.roblox.com/asset/?id=ID HERE” --Where It Says ID HERE, Place Your Animation Id
local Player = game.Players.LocalPlayer
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.KeyDown:connect(function (key)
for i, v in pairs(game.Players:GetChildren()) do
if key == “f” then – THIS IS UR KEY U WANA CHANGE IT CHANGE F TO SOMETHING ELSE
local AnimationTrack = Player.Character.Humanoid:LoadAnimation(Animation)
AnimationTrack:Play()
end
end
end)

I don’t get what you’re trying to achieve. Can you please describe the problem again?

The problem is IDK how to ONLY make able to run that script if I’am sit on a seat.

So you want to know how to make that script run ONLY when you’re sitting?

I wanna know how to run that script if I’am sitting

Okay, then you need to make a server script as the backend of this code, or in simple words, to check if you’re sitting.

The code you provided tells you to change the animation id and you didnt

local seat = script.Parent
seat.ChildAdded:Connect(function()
– Code here
end)

is the code that when someone seats the code will run

1 Like

would be put with an local script into the seat?

1 Like

both of scripts will work with this code

1 Like

no, when someone sits in a seat it doesnt add anything into it
seat has a Seated event or you can check for propertychangedsiganl of occupant

1 Like

there is something called “SeatWeld” when someone sits

1 Like

oh didnt know that
but i would still use my method so you can check who sat in the seat at the same ttime

1 Like

yes ur code was right too, thanks

local seat = script.Parent
seat.ChildAdded:Connect(function()
Animation = Instance.new(“Animation”)
Animation.Parent = game.Workspace
Animation.AnimationId = “http://www.roblox.com/asset/?id=7006749762” --Where It Says ID HERE, Place Your Animation Id
local Player = game.Players.LocalPlayer
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.KeyDown:connect(function (key)
for i, v in pairs(game.Players:GetChildren()) do
if key == “f” then – THIS IS UR KEY U WANA CHANGE IT CHANGE F TO SOMETHING ELSE
local AnimationTrack = Player.Character.Humanoid:LoadAnimation(Animation)
AnimationTrack:Play()
end
end
end)
end)
is what i did and doesnt works

1 Like

can u send the code as screenshot? i cant see what in the script actually

You can add a function that will run if Humanoid of you character sitting.

local function onSit()
    -- Code
end

game.Players.Localplayer.Character.Humanoid.Seated:Connect(onSit)
2 Likes

it just works when someone sits , he is making a special seat for every animation , this code will work for every seat

1 Like

local function onSit()
local seat = script.Parent
seat.ChildAdded:Connect(function()
Animation = Instance.new(“Animation”)
Animation.Parent = game.Workspace
Animation.AnimationId = “http://www.roblox.com/asset/?id=7006853886” --Where It Says ID HERE, Place Your Animation Id
local Player = game.Players.LocalPlayer
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.KeyDown:connect(function (key)
for i, v in pairs(game.Players:GetChildren()) do
if key == “X” then – THIS IS UR KEY U WANA CHANGE IT CHANGE F TO SOMETHING ELSE
local AnimationTrack = Player.Character.Humanoid:LoadAnimation(Animation)
AnimationTrack:Play()
end
end
end)
end)
end

game.Players.Localplayer.Character.Humanoid.Seated:Connect(onSit)

For some reason it doesn’t works when I sit an I press X.

If you need to sit and play animation after pressing X. You need to add UserInputService or ContexActionService. Read more about them on DevHub (link).
And, please, use special code box for code.