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)
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.
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.
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
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.
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
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.)
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.
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
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