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

How to get UserInputService to change int/bool/string values?

I'm trying to recreate the VehicleSeat by using a normal Seat. It has intvalues inside that I can use to do things beyond what a VehicleSeat can do (for instance, a value for upwards/downwards movement, or even some sort of gearshift).

Here's the script currently.

w = Enum.KeyCode.W
s = Enum.KeyCode.S

Seat = script.Parent
Thrust = Seat.Thrust.Value
-------------------------------------------
function ThrustControl(inputObject, gameprocessedEvent)
	if inputObject.KeyCode == w then
		Thrust = 1
	elseif inputObject.KeyCode == s then
		Thrust = -1
	elseif inputObject.KeyCode ~= w or s then
		Thrust = 0
	end
end
-------------------------------------------
game:GetService("UserInputService").InputBegan:connect(ThrustControl)

When I test it out, the thrust value should be changing its' value when I press the W or S key, and also reverting to zero when I'm not pressing either. It doesn't change at all.

Anybody know why this doesn't work?

1 answer

✓ Accepted answer

On line 05 you defined Thrust as a value. Therefore when you set that value it doesn't set the actual property of the the object, all that defining it did is get the value so it isn't changing the value.

Seat = script.Parent
Thrust = Seat.Thrust.Value --Defining the value of a property

To fix the code simply do the following:

w = Enum.KeyCode.W
s = Enum.KeyCode.S

Seat = script.Parent
Thrust = Seat.Thrust --Define the value object!
-------------------------------------------
function ThrustControl(inputObject, gameprocessedEvent)
    if inputObject.KeyCode == w then
        Thrust.Value = 1 --Set value
    elseif inputObject.KeyCode == s then
        Thrust.Value = -1 --Set value
    elseif inputObject.KeyCode ~= w or s then
        Thrust.Value = 0 --Set value
    end
end
-------------------------------------------
game:GetService("UserInputService").InputBegan:connect(ThrustControl)

4 comments

0

Ah thanks, that fixed it!

0

Please click "Accept Answer" to close the question and prevent further responses.

0

Where is the button for that? I don't see it...

0

It should be somewhere under my name or something on my answer.

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