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

My gun modes wont swap for some reason?

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

1 comment

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.

1 answer

✓ Accepted answer

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)

1 comment

0

Thanks this answered it.

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