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

My gun modes wont swap for some reason?

Asked by 5 years ago

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.

01local 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
0
I explained that it wouldn't swap. There's no errors its just trashy coding, the location of the script is in a gun tool. oshawat00 62 — 5y

1 answer

Log in to vote
1
Answered by
rexhawk 222 Moderation Voter
5 years ago
Edited 5 years ago

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.

01if 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
15end

(Also I see you're getting the key "z" as a string, but it's better practice to be using UserInputService)

0
Thanks this answered it. oshawat00 62 — 5y
Ad

Answer this question