How would I pause/stop a player's function or script?

I am trying to make a stun effect that would also pause/stop/cancel a player’s attack. I have looked through articles and I have tried using coroutine.yield or something but I can’t do it from the outside. I also don’t know how to pause or stop a function or something.

Example of what I want: For example, a player uses a stunning move that freezes the other player for a second (I can stop movement obviously) but, the other player is still able to attack which is what I don’t want. Like, you know how in Black Magic 2 where you attack someone and their attack is canceled and stopped in mid time? That is what I want to achieve. All players use keybinds through local scripts, which fires to a remote event to server side. How would I pause the other player’s script or cancel it so their attack just stops and cancels? Please I need help and I am confused. This is the server side scripts and I am trying to cancel it in mid-time

Also, this is on server side, so disabling the script will disable all player’s attacks.

Scripts:

event.OnServerEvent:Connect(function(player)

-- attack stuff

local debounce = false

attack.Touched:Connect(function(hit)
local enemyhumanoid = hit.Parent:FindFirstChild("Humanoid")
if debounce == false and enemyhumanoid and enemyhumanoid.Health > 0 then
debounce = true

enemyhumanoid:TakeDamage(10)
enemyhumanoid.WalkSpeed = 0 -- i have other ways to stop the player from moving this is just an example
enemyhumanoid.JumpPower = 0-- i have other ways to stop the player from moving this is just an example

-- enemy attack stop?

end
end)

end)

some ways I thought of was putting a variable to see if the player was stunned or not, then it would skip over the attack. But, the problem is I would need like 100 if statements in this function.

I need help, please help, and you can alter/change/suggest what to change to the script or an efficient way but I want it to be able to stop a player’s attack in mid time.

3 Likes

You can insert a boolean value into the player’s character through Instance.new(). That will be named CanAttack
Basically

CanAttack = Instance.New("BooleanValue", character)

Now, you can put inside the character script, that if only canattack is true, the player can attack using an if statement.

if Character.CanAttack.Value == true then
    -- Attack
end

Then after the other player gets stuneed, you get other players Canattack and then set its value to false.

if debounce == false and enemyhumanoid and enemyhumanoid.health > 0 then
    CanAttack.Value = false
    -- and other stuff
end

And after he is unstunned, you can again set that value to true.

yeah but how do you stop them from attacking if they were already attacking? I would have to put if statements on each attack part, so what you said is the problem i described, too many if statements and it would be over EVERY single line. If the player was already attacking, this would not do anything.

1 Like

Try this:-
Put this inside the functon which attacks.

CanAttack:GetPropertyChangedSignal("Value"):Connect(function()
    return return --not sure if this will work but do try
)

Sorry for the late replies. Im busy(Kinda)

whats GetPropertyCHanged Signal(“Value”)?

1 Like

It gets when a property of an object is changed, that function fires

1 Like

why would i use return return? what will it do

1 Like

return breaks a function.
ur returning a return so it will break both functions.

You can use the function above to get an animation track playing on a character or store the value returned by LoadAnimation.

You can then use the function above to stop the animation track. Set the first parameter to 0 to make it stop instantly.

To control the stun, I’d recommend adding an Instance to the player, character, or humanoid (you can also use a BooleanValue as suggested above or a BindableEvent). You can then connect the Instance.ChildAdded event to a function that checks for something named a specific name (ex. “StunnedEffect”). If that happens you can signal weapon code to stop and end connections (touched events, etc.).

dude… theres a red line under the second return

and how would i end the connections tho? thats my whole question

Ill try to find a solution. I was not really sure it would work.

You can pause threads by busy waiting like this

--Code 
local condition = script.Parent.PauseStatus.Value
while true do
 if not condition then
   break
 end
 wait()
end

Oh okay, do something like this:
local connection = part.Touched:Connect(onTouched)
wait(3)
connection:Disconnect()

You need to store the value returned by the connect function.
Written on mobile, sorry for the lack of formating

Edit:

Helpful Resources:

An event (in Roblox) is formally called a RBXScriptSignal:

When you connect an event it returns a connection (aka RBXScriptConnection):

You can then call :Disconnect on this connection to disconnect it (note this stops more threads from being created, doesn’t stop existing ones).

my function isnt in a while true do loop

He is using a function not a while loop. break() only works for loops

disconnect? what does that mean? you can disconnect functions?

You can disconnect event listeners, look into rbx event signals

1 Like

https://developer.roblox.com/en-us/recipes/How-to-disconnect-an-event-connection

so i would disconnect the function if a boolvalue changed to false or something? or a while true do loop checking it?