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

Weapon Mode Changing Isn't Working?

basically what i mean is , it does function correctly ish, but just here look

uis.InputBegan:connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		gun.Mode:Play()
		if mode == "auto" then
			mode = "burst"
			print(mode)
		end
		wait(.3)
		if mode == "burst" then
			mode = "semi"
			print(mode)
		end
		wait(.3)
		if mode == "semi" then
			mode = "auto"
			print(mode)
		wait(.3)
		end
	end
end)

output- burst semi auto

i don't know why it is doing all of them at once can someone help me.

2 answers

✓ Accepted answer

Wrote this a little late on mobile, so sorry if I sound weird. :P

Okay, so the problem is your if statements. They are separate. When you change the mode to “burst”, it moves on to your next statement and sees that the mode is “burst”. Now the mode changes to “semi”, then it moves on to the next statement. Since the mode is now “semi”, the mode changes back to “auto”. See what I mean?

You can easily fix this by using elseif, which I guess “combines” all your statements together. Here’s what your code should look like :

uis.InputBegan:connect(function(key)
    if key.KeyCode == Enum.KeyCode.E then
        gun.Mode:Play()
        if mode == "auto" then
            mode = "burst"
            print(mode)
        elseif mode == "burst" then
            mode = "semi"
            print(mode)
        elseif mode == "semi" then
            mode = "auto"
            print(mode)
        end
    end
end)

Now when a statement equals to something, it’ll skip the rest of the if statements and wait for the event to fire again, instead of running through all of the statements and well, you know.

Hope this helped!

1 comment

0

thank you!!!

Here is the issue, you used separate "if" statements for each. This is changing the value, therefore making the value in the next statement true. To fix this, we would have one "if" statement, with other "elseif"'s inside it. Basically, in the first statement it is "auto" is changing to "burst". In the next statement, it checks for burst again, which per your last statement is now true. Anywho, this is an easy fix. Here ya go:

uis.InputBegan:connect(function(key)
    if key.KeyCode == Enum.KeyCode.E then
        gun.Mode:Play()
        if mode == "auto" then
            mode = "burst"
            print(mode)
        elseif mode == "burst" then
            mode = "semi"
            print(mode)
        elseif mode == "semi" then
            mode = "auto"
            print(mode)
        end
    end
end)

3 comments

0

Didn't refresh to see if someone had already answered, oh whale.

0

Well, you explained it better than me in my opinion at least. :P

0

If you say so, I spent 4 minutes trying to word it so it could be better understood, and I thought I was unsuccessful.

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