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

Toggle Status not doing what I want it to?

I'm trying to make it so that if you press E, your stance will be passive, and if you press E again your stance will be aggressive. If I press E, the game successfully changes the status to "Passive", but if I press it again the status stays at "Passive".

local Player = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait()
local mode = Instance.new("StringValue", char.Torso)
local mouse = Player:GetMouse()
wait(1)
mode.Name = "mode"
local GUI = Instance.new("ScreenGui", Player.PlayerGui)  -- //\\//ACTION TOGGLE VISUAL\\//\\ --
GUI.Name = "FDF"
local PA = Instance.new("TextButton", GUI)
PA.Size = PA.Size +UDim2.new(0,100,0,25)
PA.Draggable = true
PA.Text = "Status (E/<Attacks>)"
PA.Position = PA.Position +UDim2.new(0,1250,0,25)
script.Disabled = false
-- VARIABLES ABOVE --

-- FUNCTION BELOW --
function onKeyDown(key) -- ACTION TOGGLE --
	
	key = key:lower()

	if key == "e" then

		mode.Value = "Passive"
				PA.Text = mode.Value
		if mode.Value == "Passive" then

					char.Animate.idle.Animation1.AnimationId = "http://www.roblox.com/asset/?id=700567206"
					char.Animate.idle.Animation2.AnimationId = "http://www.roblox.com/asset/?id=700567206"
			char.Humanoid.WalkSpeed = 8
			end -- ends 'if mode == passive'
		end -- ends if
end -- ends function
function onKeyDown2(key) -- ACTION TOGGLE --

	if key == "e" then
		mode.Value = "Aggressive"
				PA.Text = mode.Value
			char.Humanoid.WalkSpeed = 25
		end -- ends if
	end -- ends function
	
	


mouse.KeyDown:connect(onKeyDown)
mouse.KeyDown:connect(onKeyDown2)

Any solutions?

1 answer

✓ Accepted answer

You should make sure you are using a local script on the client first. Then I would recommend you do something like this to get input for E:

local is_passive = true;
local debounce = false;
local function change_passive()
	if not debounce then
		debounce = true;
		if is_passive then
			--change the character accordingly
			is_passive = false;
		else
			--change the character accordingly
			is_passive = true;
		end
		wait(0.2);
		debounce = false;
	end
end
game:GetService("ContextActionService"):BindAction("change_mode", change_passive, false, Enum.KeyCode.E);

it is better to use ContextActionService and UserInputService for user input rather then the client's mouse. I hope this helps

2 comments

0

Interesting suggestion but no cigar. It still doesn't toggle back and forth it only runs the first button press.

0

It should toggle back and forth according to what I tested. I would recommend increasing the wait time on line 13 just in-case you were holding the key for too long as I did my first go around.

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