Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to trigger a script when someone sits on a seat?

I am making an exercising bike and I made this local script inside the seat. It's supposed to detect when someone touches the seat. After that, it will launch the OnTouched function. The OnTouched function prints function activated after 3 seconds to show that the function has been activated. After that is printed, it will check if the player is sitting, and if so, it will print to show that it works and then gives them money. At the moment the output says nothing and also doesn't print anything.

local Player = game.Players.LocalPlayer --Get the player's character here
local money = game.Players.LocalPlayer.leaderstats.SeerBucks


local function OnTouched(player)
	wait(3)
	print("function activated")
	if Player.Humanoid.Sit == true then
		print("If Worked")
		money.Value = money.Value + 100
	end
end

script.Parent.Touched:Connect(OnTouched)

3 comments

0

So do you want it to call script when seat is touched or when someone sits on it?

0

Seats have the Occupant property so maybe you can listen for changes on that instead of using Touched..

0

Sorry, should have specified. I want it to trigger the script when they are sitting

2 answers

Local Script

local Player = game.Players.LocalPlayer
local stats = Player:WaitForChild("leaderstats")
local money = stats:WaitForChild("SeerBucks")
repeat wait() until Player.Character ~= nil and workspace:FindFirstChild(Player.Name)
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local debounce = true

Humanoid.Changed:Connect(function()
	if Humanoid.Sit == true and Humanoid:GetState() == Enum.HumanoidStateType.Seated and debounce == true then
		debounce = false
		print("It Worked")
		money.Value = money.Value + 100
	elseif Humanoid.Sit == false and Humanoid:GetState() ~= Enum.HumanoidStateType.Seated and debounce == false then
		debounce = true
	end
end)

Since you want this to fire anytime the player sits down, we will be basing our function on the Humanoid.Changed Event rather than when the player touches a part

In addition to Humanoid.Sit there is also a HumanoidStateType that you can check to see if the player is seated.

The debounce and elseif statement are used so that this event is only fired once until the player has stopped sitting down.

Also, the Local Script will load before you can create the leaderstats and SeerBucks instances, so we will use WaitForChild() to wait until they are found in the player. The same applies to the player Character and its loading into the workspace.

I've managed to put together a little script for you, please note that this code should run on a regular server sided script, not a LocalScript like you originally had.

local occupant = nil -- Nobody is sitting on the seat by default.

script.Parent:GetPropertyChangedSignal("Occupant"):connect(function()
	if script.Parent.Occupant then -- If there now is an occupant
		occupant = game.Players:FindFirstChild(script.Parent.Occupant.Parent.Name)
		 --[[
			 The occupant variable is now referring to the player that is occupying the seat.
			 Note that the Occupant parameter returns the player's humanoid, not the player himself.
		--]]
	elseif not script.Parent.Occupant then -- If there is no occupant
		occupant = nil
	end
end)

while true do
	wait(3)
	if occupant then
		occupant.leaderstats.SeerBucks.Value = occupant.leaderstats.SeerBucks.Value + 100
	end
end

In future scripts please do avoid using LocalScripts for things such as awarding the player with money, those scripts are prone to exploitation because they run on the player's machine.

And even if you were to award money through the use of LocalScripts, it will not be replicated to other players and the server itself which means it can't be used to make purchases.

This discussion is preserved as it was. New answers, comments, and votes are closed.