How do i reset wait() timer

yo, i’m tryna make it so that a gun stuns a player for 1 second but also not unstun the player when the player is still getting shot at
code is not mine but i’m tryna customize it
here’s the function that needs changing

takeDamage.OnServerEvent:Connect(function(fired, hit, damage)
    local humanoid = hit
    humanoid.PlatformStand = true
    for i = 1, 10 do
    humanoid:TakeDamage(damage)
    wait(.1)
    end
    humanoid.PlatformStand = false
end)
1 Like

Nothing here is going to un-stun the player. So the un-stun must be coming from other hits.
You may need a flag to stop other hits from breaking the stun. Luckily you do have a nice “flag” being set as is … PlatformStand. So in your other hits, test if PlatformStand==true, if so exit out of that hit without it interacting with the player at all.

un stun is after the wait(.1) after the for loop ends

trying to make it so the player STAYS stunned and the wait timer resets as the player keeps getting hit

Looks like stun is during the wait loop … The “stun” would start with humanoid.PlatformStand = true …
Then hold that for 1 second while in the loop.

hmm but where would i put the part to end the stun? also like i said i don’t want the player getting up after 1 second i want the wait timer to reset back to 0 every time the player is hit

ok this was one of my questions …

Try this may need to edit it to work with your system:

local lastHitTime = {}
local stunDuration = 1

takeDamage.OnServerEvent:Connect(function(fired, hit, damage)
    local humanoid = hit
    local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)

    if not player then return end

    local playerId = player.UserId

    -- Record the last hit time
    lastHitTime[playerId] = tick()

    humanoid.PlatformStand = true

    -- Start or restart the coroutine if not already running
    if not humanoid:FindFirstChild("StunCoroutine") then
        local stunCoroutine = Instance.new("StringValue")
        stunCoroutine.Name = "StunCoroutine"
        stunCoroutine.Parent = humanoid

        coroutine.wrap(function()
            while true do
                if tick() - lastHitTime[playerId] >= stunDuration then
                    break
                end
                humanoid:TakeDamage(damage)
                wait(0.1)
            end
            humanoid.PlatformStand = false
            stunCoroutine:Destroy()
        end)()
    end
end)

not tested but should work, also implemented delta time based damaging


local takeDamageThreads = {}
takeDamage.OnServerEvent:Connect(function(fired, humanoid, damage)
    if takeDamageThreads[humanoid] then
       task.cancel(takeDamageThreads[humanoid])
       takeDamageThreads[humanoid]  = nil
    end
    takeDamageThreads[humanoid] = task.spawn(function()
        humanoid.PlatformStand = true
        local start = os.clock()
        local timeDamaging = 1 -- 0.1 x 10
        while os.clock() - start < timeDamaging do
            local dt = task.wait(0.1)
            humanoid:TakeDamage(damage * dt * 60)
        end
        humanoid.PlatformStand = false
    end)
end)

This here is pretty much what I did … that for next style of damage is really messing me up.
No need to post the same approach. This would be way easier if the damage loop was on a countdown and checked/updated the count each time through the loop. That way you could just add time on every hit to the countdown.

A short example of this.

local countdownValue = 10
local function startCountdown()
	while countdownValue > 0 do
		print(countdownValue)
		countdownValue -=1
		wait(1)
	end
	if countdownValue <= 0 then
		print("Finished")
	end
end

local function updateCountdown(newValue)
	countdownValue = countdownValue + newValue
end

task.spawn(function()
	startCountdown()
end) 

task.wait(3)
updateCountdown(15)

os.clock is meant for benchmarking. Gameplay related timings should use workspace:GetServerTimeNow() (good for client code), workspace.DistributedGameTime (server), or the same figure returned from RunService events.

2 options: module that uses something like os.time to compare time remaining with new time in order to keep the stun on the player
OR
option 2: create a folder inside a player, and if it has a child named stun in it, the player is stunned, so basically you can have more than one stun instance in the folder, and the player will stay stunned until all go away, so basically when he stops getting shot

good module for stuns that work like that is stunhandlerv2 (i think thats the name) and its very easy to use

thanks for letting me know, i’m now using stun handler in my game and it works just like i want it to

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