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 4 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.

local mode = script.Parent.Modes.Mode.Value
            if key == "z" and reloading == false and firing == false then
                if mode == "Damaging" then
                    mode = "Stun"
                    print("Stun")
            elseif mode == "Stun" then
                mode = "Poison"
                print("Poison")
            elseif mode == "Poison" then
                mode = "Damaging"
                print("Damaging")
            else
                mode = nil
            end
            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 — 4y

1 answer

Log in to vote
1
Answered by
rexhawk 222 Moderation Voter
4 years ago
Edited 4 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.

if key == "z" and reloading == false and firing == false then
    local mode = script.Parent.Modes.Mode
    if mode.Value == "Damaging" then
        mode.Value = "Stun"
        print("Stun")
    elseif mode.Value == "Stun" then
        mode.Value = "Poison"
        print("Poison")
    elseif mode.Value == "Poison" then
        mode.Value = "Damaging"
        print("Damaging")
    else
        mode.Value = ""
    end
end

(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 — 4y
Ad

Answer this question