Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 8 years ago

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.

01w = Enum.KeyCode.W
02s = Enum.KeyCode.S
03 
04Seat = script.Parent
05Thrust = Seat.Thrust.Value
06-------------------------------------------
07function ThrustControl(inputObject, gameprocessedEvent)
08    if inputObject.KeyCode == w then
09        Thrust = 1
10    elseif inputObject.KeyCode == s then
11        Thrust = -1
12    elseif inputObject.KeyCode ~= w or s then
13        Thrust = 0
14    end
15end
16-------------------------------------------
17game: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

Log in to vote
3
Answered by 8 years ago

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.

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

To fix the code simply do the following:

01w = Enum.KeyCode.W
02s = Enum.KeyCode.S
03 
04Seat = script.Parent
05Thrust = Seat.Thrust --Define the value object!
06-------------------------------------------
07function ThrustControl(inputObject, gameprocessedEvent)
08    if inputObject.KeyCode == w then
09        Thrust.Value = 1 --Set value
10    elseif inputObject.KeyCode == s then
11        Thrust.Value = -1 --Set value
12    elseif inputObject.KeyCode ~= w or s then
13        Thrust.Value = 0 --Set value
14    end
15end
16-------------------------------------------
17game:GetService("UserInputService").InputBegan:connect(ThrustControl)
0
Ah thanks, that fixed it! bradgangsta11 20 — 8y
0
Please click "Accept Answer" to close the question and prevent further responses. EssentialRoll 30 — 8y
0
Where is the button for that? I don't see it... bradgangsta11 20 — 8y
0
It should be somewhere under my name or something on my answer. EssentialRoll 30 — 8y
Ad

Answer this question