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

UserInputService is still running even tho im out of the car?

Hello, i need help with UserInputService. the script is inside a car's vehicle seat. When i test the game normally, it doesnt run the script. but if i enter the car, the script runs as its supposed to be. But if i exit the car, UserInputService is still runnng. Whats the problem, how can i stop it form doing that? heres my script:

local debounce = true
local uis = game:GetService("UserInputService")
		local h1 = script.Parent.Parent.H1
		local h2 = script.Parent.Parent.h2
		local b1 = script.Parent.Parent.b1
		local b2 = script.Parent.Parent.b2


script.Parent.Changed:connect(function()
if script.Parent.Throttle == 0 or script.Parent.Throttle == -1 then
	b1.PointLight.Enabled = true		
	b2.PointLight.Enabled = true

	script.Parent.Value.Value = 20
end
	if script.Parent.Throttle == 1 or script.Parent.ThrottleFloat == 1 then
	b1.PointLight.Enabled = false		
	b2.PointLight.Enabled = false

		script.Parent.Value.Value = 300
end
end)

script.Parent.ChildRemoved:Connect(function()
		b1.PointLight.Enabled = false		
	b2.PointLight.Enabled = false
	debounce = true
	script.Disabled = true
end)

script.Parent.ChildAdded:Connect(function()
	script.Disabled = false
	script.Parent.Sound:Play()
	debounce = false
	uis.InputBegan:connect(function(key)
	if key.KeyCode == Enum.KeyCode.L then
	h1.SpotLight.Enabled = not h1.SpotLight.Enabled
	h2.SpotLight.Enabled = not h2.SpotLight.Enabled
end
end)
uis.InputBegan:connect(function(key)
	if key.KeyCode == Enum.KeyCode.X and not debounce then
		debounce = true
local d = Instance.new("BodyForce")
d.Force = Vector3.new(0,50000,0)
d.Parent = script.Parent.Parent.base
local b = Instance.new("BodyGyro")
b.P = Vector3.new(5000)
b.D = Vector3.new(10000)
b.MaxTorque = Vector3.new(500, 100, 100)
b.Parent = script.Parent.Parent.base
wait(0.5)
d:Destroy()
b:Destroy()
wait(3)
debounce = false
end
end)
end)

1 answer

✓ Accepted answer

Your best bet is to add an IsInCar boolean value and make it true. So whenever you leave the car, make that value false and in the if statement for the KeyCode, make another if statement saying:

if IsInCar == false then
	return end

This should hopefully stop the code being executed when you leave that car. REMEMBER TOO make IsInCar true when you inside the vehicle and make it false when you leave it for it to function properly. This will most probably prevent code inside the Input Event from being executed when you leave that car. Hope you have a good drive! (See what I did there).

1 comment

0

Wow, thank you so much, it worked! I really need to learn how to use return.

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