Vehicle Seat lock Script Not Working

So what im trying to do is make it so when the button is clicked inside the vehicle gui, the character who is sitting in the seats labled in ‘seatparts’ make it so that they cannot get out of their seats, this is so when the driver is driving no one gets up and takes the bus flying because of roblox physics.

Ive tried by getting the humanoid’s jumppower set to 0 however when testing in studio it doesnt seem to work.

SLock.MouseButton1Click:Connect(function()
		local lt = 1
		if lt == 1 then
			lt = 2
			TweenService:Create(SLock.TextButton_Roundify_12px, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {ImageColor3 = Color3.fromRGB(0, 0, 0)}):Play()
			local function disableLeaveSeat(humanoid)
				humanoid.JumpPower = 0
			end
			seats.S1:GetPropertyChangedSignal("Occupant"):Connect(function()
				if seats.S1.Occupant ~= nil then
					local character = seats.S1.Occupant.Parent
					if game.Players:GetPlayerFromCharacter(character) ~= nil then
						disableLeaveSeat(character.Humanoid)
					end
				end
			end)
		else
			lt = 1
			TweenService:Create(SLock.TextButton_Roundify_12px, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {ImageColor3 = Color3.fromRGB(36, 36, 36)}):Play()
			local function disableLeaveSeat(humanoid)
				humanoid.JumpPower = 50
			end
			seats.S1:GetPropertyChangedSignal("Occupant"):Connect(function()
				if seats.S1.Occupant ~= nil then
					local character = seats.S1.Occupant.Parent
					if game.Players:GetPlayerFromCharacter(character) ~= nil then
						disableLeaveSeat(character.Humanoid)
					end
				end
			end)
		end
	end)

2 Likes

Setting the player’s jump power / jump height to 0 should make them unable to leave the seat, but you said that it wasn’t working. Could you specify what happens then?

I noticed a few odd things in your code and commented on them below:

SLock.MouseButton1Click:Connect(function()
	local lt = 1 -- you are setting lt to equal 1
	if lt == 1 then -- now you are checking it is equal to 1 (it always will be since you just set it to 1)
		lt = 2 -- now you are changing it to be eqaul to 2
		TweenService:Create(SLock.TextButton_Roundify_12px, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {ImageColor3 = Color3.fromRGB(0, 0, 0)}):Play()
		local function disableLeaveSeat(humanoid)
			humanoid.JumpPower = 0
		end
		seats.S1:GetPropertyChangedSignal("Occupant"):Connect(function()
			if seats.S1.Occupant ~= nil then
				local character = seats.S1.Occupant.Parent
				if game.Players:GetPlayerFromCharacter(character) ~= nil then
					disableLeaveSeat(character.Humanoid)
				end
			end
		end)
	else
		lt = 1 -- it aready eqauls 1 (you set it to be equal to 1 above so this code will never run)
		TweenService:Create(SLock.TextButton_Roundify_12px, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {ImageColor3 = Color3.fromRGB(36, 36, 36)}):Play()
		local function disableLeaveSeat(humanoid)
			humanoid.JumpPower = 50
		end
		seats.S1:GetPropertyChangedSignal("Occupant"):Connect(function()
			if seats.S1.Occupant ~= nil then
				local character = seats.S1.Occupant.Parent
				if game.Players:GetPlayerFromCharacter(character) ~= nil then
					disableLeaveSeat(character.Humanoid)
				end
			end
		end)
	end
end)

I also have discovered that GetPropertyChangedSignal frequently fails. So maybe try using seats.S1.Changed instead.

2 Likes

Sorry for the late reply.
changed it to .changed and nothing happened. When doing a Print debug it didnt end up showing so i dont believe its getting to that point of the script.

SLock.MouseButton1Click:Connect(function()
		if lt == 1 then -- now you are checking it is equal to 1 (it always will be since you just set it to 1)
			TweenService:Create(SLock.TextButton_Roundify_12px, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {ImageColor3 = Color3.fromRGB(0, 0, 0)}):Play()
			local function disableLeaveSeat(humanoid)
				humanoid.JumpPower = 0
			end
			seats.S1.Changed:Connect(function()
				if seats.S1.Occupant ~= nil then
					local character = seats.S1.Occupant.Parent
					if game.Players:GetPlayerFromCharacter(character) ~= nil then
						disableLeaveSeat(character.Humanoid)
						print("Locked")
					end
				end
			end)
		else
			lt = 2
			TweenService:Create(SLock.TextButton_Roundify_12px, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {ImageColor3 = Color3.fromRGB(36, 36, 36)}):Play()
			local function disableLeaveSeat(humanoid)
				humanoid.JumpPower = 50
			end
			seats.S1.Changed:Connect(function()
				if seats.S1.Occupant ~= nil then
					local character = seats.S1.Occupant.Parent
					if game.Players:GetPlayerFromCharacter(character) ~= nil then
						disableLeaveSeat(character.Humanoid)
						print("Unlocked")
					end
				end
			end)
		end
	end)

the player is still able to jump out of the seat. the whole reason why this script is a thing is to stop trolls making the bus go flying when driving when they arent seated