Detect value change while function is running, without pressing A again

  1. What do you want to achieve?
    I have a tank controls script for my animal model. The animal has different states: Idle, Trotting, Cantering, Sprinting etc. For each state the corresponding BoolValue named like the state turns true/false in ReplicatedStorage.

I basically want the function to recognize the value change.

  1. What is the issue?

The issue is if I turn left (see code snippet) and the idle value is true but I start walking then the idle value turns false and walking turns true logically. The script does not recognize the change while A is being held down. Only if I release A again and press it again it realizes that the value idle is now false.

Code Snippet of turning left when A is being held down -

local function OnLeft (actionName, inputState)
	if inputState == InputBegin and DPressed == false then
		APressed = true
		wait(0.07)
		turningleft = true
		if Idle.Value == true then
			leftValue = 0.5
			LeftTween:Cancel()
			RightTween:Play()
			--BaseTween:Play()
		elseif Walking.Value == true then
			leftValue = 0.5
			LeftTween:Cancel()
			RightTween:Play()
		end
	elseif inputState == InputEnd then
		APressed = false
		if turningleft == true then
			BaseTween:Play()
		end
		leftValue = 0
	end
end
1 Like

Can you send the function with the States, for an example:

local function CheckStateChanges()
    while true do
        if Idle.Value == true then

        elseif Walking.Value == true then


        end

        wait(0.1)
    end
end
1 Like

Oh it’s just a LocalScript that turns the values to true if the animations are playing. So basically if the idle animation starts playing the idle value in ReplicatedStorage turns true, same goes for walking etc. In this script I am just referring to the values as every state has a different turning radius

I fixed it!

A while loop now constantly checks the values so I don’t have to press A again everytime to update the values.

local function OnLeft (actionName, inputState)
	if inputState == InputBegin and DPressed == false then
		print("started")
		APressed = true
		wait(0.07)
		turningleft = true
		
		while turningleft == true do
			if Idle.Value == true then
				leftValue = 0.5
				LeftTween:Cancel()
				RightTween:Play()
			elseif Walking.Value == true then
				leftValue = 2
				LeftTween:Cancel()
				RightTween:Play()
			end
			wait(0.5)
		end
		elseif inputState == InputEnd then
			print("ended")
			APressed = false
			if turningleft == true then
				turningleft = false
				BaseTween:Play()
				leftValue = 0
			end
		
	end
end```

“I basically want the function to recognize the value change” why don’t you use Idle.Value.Changed:Connect…?
In your code, I don’t see you ever setting Idle.Value to true or false only checking if it is, you’re only setting the value of local variables?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.