but it should work fine in the server script service or it doesnt really matter?
Yes, it could work anywhere. Preferably it would be put in a server storage object like ServerScriptService.
Alright, I’ll keep this in mind whenever I’m making games. Thank you!
You can try a “global task” feature by making a playerStuns global table and filtering by the player’s name, including the task.spawn()
and task.cancel()
function, avoiding that the wait() “combos”:
local StunModule = {}
local playerStuns = {}
function StunModule:Stun(Character:Instance, Duration: number, StunWalkSpeed: number, StunJumpPower: number)
local Humanoid = Character:WaitForChild("Humanoid")
if Humanoid then
if Humanoid.Health <= 0 then
return
end
else
return
end
if not playerStuns[Character.Name] then
playerStuns[Character.Name] = {
WS = Humanoid.WalkSpeed,
JP = Humanoid.JumpPower
}
end
local WalkSpeed = playerStuns[Character.Name].WS
local JumpPower = playerStuns[Character.Name].JP
if playerStuns[Character.Name].Task then task.cancel(playerStuns[Character.Name].Task) end
playerStuns[Character.Name].Task = task.spawn(function()
Humanoid:SetAttribute("Stunned", true)
Humanoid.WalkSpeed = StunWalkSpeed
Humanoid.JumpPower = StunJumpPower
wait(Duration) --// This is the issue.
Humanoid:SetAttribute("Stunned", false)
Humanoid.WalkSpeed = WalkSpeed
Humanoid.JumpPower = JumpPower
warn("Ended")
playerStuns[Character.Name] = nil
end)
end
return StunModule
This will cause memory leaks make sure to clear the tables when the player leaves
Oh, I thought I added it on the code, that was my bad!
How do you achieve this? I’m not experienced with using tables…
Do you know how to clear the tables when the player leaves or after the stun is over? I don’t know how to use tables that much and I don’t wanna have memory leaks
Sure! You need to use the Players.PlayerRemoving function to clear the table. Here’s how you clear it once the player leaves or the stun is over (Full Code):
local StunModule = {}
local playerStuns = {}
local Players = game:GetService("Players")
Players.PlayerRemoving:Connect(function(player)
if playerStuns[player.Name] then playerStuns[player.Name] = nil end
end)
function StunModule:Stun(Character:Instance, Duration: number, StunWalkSpeed: number, StunJumpPower: number)
local Humanoid = Character:WaitForChild("Humanoid")
if Humanoid then
if Humanoid.Health <= 0 then
return
end
else
return
end
if not playerStuns[Character.Name] then
playerStuns[Character.Name] = {
WS = Humanoid.WalkSpeed,
JP = Humanoid.JumpPower
}
end
local WalkSpeed = playerStuns[Character.Name].WS
local JumpPower = playerStuns[Character.Name].JP
if playerStuns[Character.Name].Task then task.cancel(playerStuns[Character.Name].Task) end
playerStuns[Character.Name].Task = task.spawn(function()
Humanoid:SetAttribute("Stunned", true)
Humanoid.WalkSpeed = StunWalkSpeed
Humanoid.JumpPower = StunJumpPower
wait(Duration)
Humanoid:SetAttribute("Stunned", false)
Humanoid.WalkSpeed = WalkSpeed
Humanoid.JumpPower = JumpPower
warn("Ended")
playerStuns[Character.Name] = nil -- Clears the table for the player
end)
end
return StunModule
I suggest you reading the Tables Documentation for better knowledge and information on how to use it.
The player removing function doesn’t work idk why. I think it’s because when the player leaves the script can’t read the player’s name anymore because the player is not present.
Nvm I fixed my own problem thanks!
It won’t clear the table if it already has been cleaned (if the player is not stunned).
The way i’d do it is to measure if the stun timer is above 0 and if it is then stun
Then when a player gets hit do “stuntime.Value += 3”