I’m making a game with combat, however I’m concerned with these problems;
When a player’s punching they’re slowed
When a player’s hit they are slowed as well
The game has sprinting
If I were to use a move that slows the player down for 5 seconds, then use another move that slows them down for 1 seconds it would cancel the set speed it was at before. If I were to check if the player’s stunned still, then if I use a move that should slow the player longer it wouldn’t since the player was already hit by an attack.
What can I do to make this a simple method? Should I use a module? And if I use a module how would I manage speed correctly to where speed stacks + slows the player down more if the new stun speed is slower than the other stun speed? e.g hit a player with a punch making them 5 walkspeed then use a freeze move making them 0 walkspeed
I’m relatively new to scripting, so you’ll have to bear with me
I’d recommend using if statements and variables.
For example, If the player is hit, a “Speed-Hold” variable is set to true. Disabling the Sprint or other speed effecting code. Then wait however many seconds and set that “Speed-Hold” variable to false.
I am sure there is an easier way, but this is what my simple monkey brain used in one of my past games.
I’m pretty sure I’ve solved the issue, but let me know if I’m doing anything wrong with my code. For those of you who are curious of how to do this (if it’s a correct solution i haven’t tested) I created a module to handle speed configuring for combat/sprinting stuff like that.
local module = {}
local adjustSpeed = game.ReplicatedStorage:WaitForChild("SetSpeed")
local speeds = {}
local setspeed
local hum
local timeLength
function default(h)
local plr = game.Players.LocalPlayer
local plrSpeed = plr:FindFirstChild("WalkSpeed")
if plrSpeed then
h.WalkSpeed = plrSpeed.Value
end
end
local changeSpeed = coroutine.create(function()
if hum and setspeed and timeLength then
hum.WalkSpeed = setspeed
wait(timeLength)
if hum then
hum.WalkSpeed = default(hum)
end
end
end)
adjustSpeed.OnClientEvent:Connect(function(hum,speed,timelength)
if hum and hum.Health > 0 then
if hum:FindFirstChild("StunValue") and timelength then -- if stun value then player cant sprint
if timelength and timeLength and timelength > timelength then -- if the timelength has to be longer to interrupt last stun
setspeed = speed
coroutine.yield(changeSpeed)
coroutine.wrap(changeSpeed)
end
elseif hum:FindFirstChild("StunValue") == nil then -- if no stun value then player can sprint etc
if timelength == nil then
hum.WalkSpeed = speed
elseif timelength then
coroutine.wrap(changeSpeed)
end
end
end
end)
return module