Combat Scripting Base

I am currently working on a combat system but I’ve run into some problems that need to be fixed or the game won’t work how it should

I’ve tried multiple times to create a “Stun” effect on the player. But the issue is, whenever the player is hit 2 or more times, the stun function dosent stop and it runs it twice, causing glitches.

I’ve searched around and havent found anything that I could use for this.

Some help would be great.
(I’ve also tried adding a debounce effect, but if I add the debounce then hitting twice won’t stun them again so they can just walk out of a combo)

4 Likes

You could try setting up a system to track a player’s “state”. When the player gets stunned it can check if they’ve already been stunned and stack the effects.

2 Likes

It detects that they’ve been stunned and fires a function.

The function is just basiclly;

StunPlayer
Wait(StunTime)
UntStunPlayer

But when it stacks, that wait function still keeps going, meaning they get unstunned when they should still be stunned.

1 Like

What I kind of meant is that you should design it so that instead of stunning the player a second time, it should just extend the time the player is stunned.

1 Like

Well you can add the debounce, but make the debounce only effect the stun effect, and the combo would work no matter what

1 Like

I don’t know how you set up your variables and functions, but try doing something like this:

This script was taken from Davidii’s Bridge Defense and slightly modified.

	local function Stun (part, damage)
		if not part then return false end
		local model = part.Parent
		if not model then return false end
		local human = model:FindFirstChild("Humanoid")
		if not human then return false end
		local player = game.Players:GetPlayerFromCharacter(model)
		if not player then return false end
		if player.Stunned == true then return false end -- This assumes you used a bool value stored as a child of the player.


		human.JumpPower = 0
		human.WalkSpeed = 0
		player.Stunned = true

		wait(0.5)
	
		
		human.WalkSpeed = 16
		human.JumpPower = 50
                player.Stunned = false


		
		return true
	end
2 Likes

In the script, all I need is a way to edit the stun time. In the function, once it starts waiting that time you can’t change the time from there. For example, lets say I put the stuntime to 2 seconds. The function runs wait(2), however if I then try and change the stun time back to 2 or any other number, it just keeps waiting 2 seconds and dosent update.

1 Like

Could you possibly provide the code you’re using right now?

1 Like
if script.Stunned.Value == true then
	print("STARTED")
	AllAttacksPaused = true
	Freeze()
	StunnedTimedAnim1:Play()
	print("WAITING")
	wait(script.StunTime.Value)
	print("WAITED")
	StunnedTimedAnim1:Stop()
	Unfreeze()
	AllAttacksPaused = false
	print("FINISHED")
end
2 Likes

So the question here is what are you looking to get out of it, if you hit a stunned player do you want the stun effect to increase its time length? If that’s something you’d want you could do something along the lines of using a NumberValue or IntValue; Each time the player is it you increment said value, and either in a separate script you can just use a

AllAttacksPaused = true
StunnedTimedAnim1:Play()
repeat Number.Value = Number.Value - decrementvalue wait(timebetweendecrements) until NumberValue.Value <= 0
Unfreeze()
AllAttacksPaused = false

I’d like to note that if you took an approach like this you’d want to run the function once, as the value changes from 0 to whatever number.

2 Likes

@DrAdministration

I’ve amended the sample code you posted in your reply, I’m not sure how your code as a whole looks, but I thought something like this might work.

local stunTime = 0

local function StunPlayer()
  stunTime = stunTime + 5
  -- include standard function
end

StunPlayer()
while stunTime > 0 do
   wait(1)
   stunTime == stunTime - 1
end
if stunTime <= 0 then
   -- unstun player OR create an UnStun function
end

Let me know if this works or not. (I have amended this reply a few times to fix any complications that might arise from what I initially presented you with.)

1 Like

Im trying this so far.

It seems to be working for now. I’ll add a few snip bits and if all goes well I’ll mark it as the solution. Thanks for the help even if it was an easy solution. I don’t use repeat until that often as sad as it is so I never think to go for it.

5 Likes

I made a system like that, use modulescript with a table, so when player be stunned you save on table, and ALL times player Will be stunned you check if he is already stunned

1 Like

There’s a really easy fix for this
Sorry for the late reply…

-- Make sure the animation is looped :D
local anim = script.StunnedAnimation -- This animation will play when a player is stunned
function stun(targetHum, stunDuration)
        for i, playingAnims in pairs(targetHum:GetPlayingAnimationTracks()) do
              if playingAnims.Name == anim.Name then
                  playingAnims.Stopped:Wait()
              end
        end
        local stunPlay = targetHum:LoadAnimation(anim)
        stunPlay:Play()
        targetHum.WalkSpeed = 0
        wait(stunDuration) 
        stunPlay:Stop()
end
-- Example on how to use it ( For example you already obtained the humanoid of the target
stun(targetHumanoid, 10)
-- If there is any bugs, please reply on what the bug is thanks! :D
1 Like