How to make one Event wait for another Event?

On the server, when a character is sprinting, I edit an instance in their starter pack called Combat and change the string value to whatever they’re doing.

I want to make it so whenever the character is sprinting, and they block, it cancels their sprint. The only problem is that when it cancels their sprint and I toggle the sprint again, it doesn’t speed them up. Instead, it toggles back off and I have to press it again to sprint.

I tried making a changed event to detect when it changes to then turn the sprint toggle back to off, but that didn’t work. How do I do this?

--Variable:

local remote = game.ReplicatedStorage.PyrSprint

local activated = false

local TweenService = game:GetService("TweenService")

--Settings:

--Script:

remote.OnServerEvent:Connect(function(player)
	print(player,"has reached the server")
	local char = player.Character 
	CombatStatus = char.Combat
	
	local BaseSpeed = char.BaseSpeed
	char.Humanoid.WalkSpeed = BaseSpeed.Value

	if activated == false and  CombatStatus.Value ~= "Blocking" and CombatStatus.Value ~= "Dashing" then
		
		local Run = {}
		Run.WalkSpeed = BaseSpeed.Value * 1.5

		local tweenInfo = TweenInfo.new(1.15,Enum.EasingStyle.Quad,Enum.EasingDirection.In)

		local RunTween = TweenService:Create(char.Humanoid, tweenInfo, Run)

		RunTween:Play()
		CombatStatus.Value = "Sprinting"
		activated = true
		
	elseif activated == true  then
		CombatStatus.Value = "None"
		char.Humanoid.WalkSpeed = BaseSpeed.Value
		
		
		print("Skip this")
		
		activated = false
	end
	
end)	

repeat wait() until activated == true
CombatStatus.Changed:Connect(function()
	print("This is running")
	print(CombatStatus.Value)
	if CombatStatus.Value == "Blocking" then
		print("its running when I block")
		activated = false
	end
end)
1 Like

I’m also having this problem, i dont know if its a good idea to put a changed event in a remote function

I know but I can’t think of another way. I tried putting it in a wait function and all sorts of things but nothing worked.

Seems like nobody else knows the solution either :frowning:

You could make a variable containing the event listening to the sprint event and make a variable which contains a string about the state of the character, which can be either sprinting or blocking or nil which will be used for checking if the player is blocking when the sprinting event is fired, and then after that, you make a variable inside the function that handles the sprinting event which will listen for the blocking event, and when it happens, you disconnect the sprint listening event along with changing the state of the variable to Blocking, and then once you’re done blocking, you disconnect the blocking event and then change the state back to nil, and whenever the player is sprinting set the state to sprinting and set it back to nil once they are done sprinting.

I don’t know can this help you, but I’ve did this myself and it works pretty well. What I do is just to create a RemoteEvent and name it UpdateStatus. After that create a Script which run on fired.

local Players = game.Players

local Events = game.ReplicatedStorage.Events

Events.UpdatePlayerStats.OnServerEvent:Connect(function(player)
	if player.Character ~= nil then
		local PlayerCharacter = player.Character
		local PlayerHumanoid = PlayerCharacter:FindFirstChild("Humanoid")

		local PlayerData = player:FindFirstChild("PlayerData")
		local PlayerEvents = player:FindFirstChild("PlayerEvents")

        if PlayerEvents ~= nil then
			if PlayerEvents.Stunned.Value == true then
				PlayerHumanoid.WalkSpeed = 0
				PlayerHumanoid.JumpPower = 0
				PlayerEvents.Blocking.Value = false
			elseif PlayerEvents.Blocking.Value == true then
				PlayerHumanoid.WalkSpeed = 4
				PlayerHumanoid.JumpPower = 0
				PlayerEvents.Barrage.Value = false
			elseif PlayerEvents.Barrage.Value == true then
				PlayerHumanoid.WalkSpeed = 11
				PlayerHumanoid.JumpPower = 0
			elseif PlayerEvents.Slowed.Value == true then
				PlayerHumanoid.WalkSpeed = 2
				PlayerHumanoid.JumpPower = 10
			elseif PlayerEvents.Running.Value == true then
				PlayerHumanoid.WalkSpeed = 20
				PlayerHumanoid.JumpPower = 50
			else
				PlayerHumanoid.WalkSpeed = 13
				PlayerHumanoid.JumpPower = 50
			end
		end
    end
end

After that, create a LocalScript inside StarterGui

local player = game.Players.LocalPlayer

local playerEvents = player:WaitForChild("PlayerEvents")

local function Update()
	Events.UpdatePlayerStats:FireServer()
end

for _, Value in pairs(playerEvents:GetChildren()) do
	Value.Changed:Connect(Update)
end

What you have to do is just to see which Status is more needed, like giving them Priority.

1 Like