Im making a gun for myself and have a mode setup however the piece of my code that should switch the mode / value of the StringValue of my gun doesn't seem to want to change it, is this because its a LocalScript or have I just messed up the coding help please. The code includes when I repeatedly press the key "z" it cycles through modes but it stays on the damaging mode.
01 | local mode = script.Parent.Modes.Mode.Value |
02 | if key = = "z" and reloading = = false and firing = = false then |
03 | if mode = = "Damaging" then |
04 | mode = "Stun" |
05 | print ( "Stun" ) |
06 | elseif mode = = "Stun" then |
07 | mode = "Poison" |
08 | print ( "Poison" ) |
09 | elseif mode = = "Poison" then |
10 | mode = "Damaging" |
11 | print ( "Damaging" ) |
12 | else |
13 | mode = nil |
14 | end |
15 | end |
You're only getting the mode value once and not setting the value correctly.
If you read the mode
variable whenever you press 'z', and set the value correctly, this should fix the problem.
01 | if key = = "z" and reloading = = false and firing = = false then |
02 | local mode = script.Parent.Modes.Mode |
03 | if mode.Value = = "Damaging" then |
04 | mode.Value = "Stun" |
05 | print ( "Stun" ) |
06 | elseif mode.Value = = "Stun" then |
07 | mode.Value = "Poison" |
08 | print ( "Poison" ) |
09 | elseif mode.Value = = "Poison" then |
10 | mode.Value = "Damaging" |
11 | print ( "Damaging" ) |
12 | else |
13 | mode.Value = "" |
14 | end |
15 | end |
(Also I see you're getting the key "z" as a string, but it's better practice to be using UserInputService)