I’m having some issues with my “DamageScript”. I made an function called swing() that fires when the player gets near the monster, and it works, BUT it doesn’t hits anything under it, and since the players ragdolls on low health, the monster cant really hit the player.
local function swing() --Fires when Monster swings (every 1 second)
for i = -5,5,1 do
local origin = rootPart.Position
local lv = rootPart.CFrame.LookVector
local rv = rootPart.CFrame.RightVector
local direction = lv * 5 + rv * i
--local direction = Vector3.new(0, -1, 0)
local result: RaycastResult? = workspace:Raycast(origin,direction,raycastParams)
if result then
local hit: BasePart = result.Instance
local model = hit:FindFirstAncestorWhichIsA("Model")
local humanoid = model and model:FindFirstChildWhichIsA("Humanoid")
if humanoid then
math.randomseed(tick()) -- Let's make it even more random
local r = math.random(1,2)
if humanoid.Health > 35 then
script.Parent.Torso.Swing:Play()
script.Parent.Torso.Hit:Play()
animations[string.format("Swing%s",tostring(r))]:Play()
humanoid:TakeDamage(config.Damage.Value)
break
else
script.Parent.Torso.HeavyHit:Play()
animations[string.format("HeavySwing%s",tostring(r))]:Play()
humanoid:TakeDamage(50)
break
end
end
end
end
end
How would I make the monster hit players under him?
If I had to guess the delay is likely due to the time it takes for the server to register the damage and apply it to your character’s health.
local function swing()
for i = -5,5,1 do
local origin = rootPart.Position
local lv = rootPart.CFrame.LookVector
local rv = rootPart.CFrame.RightVector
local direction = lv * 5 + rv * i - Vector3.new(0, 1, 0)
local result: RaycastResult? = workspace:Raycast(origin,direction,raycastParams)
if result then
local hit: BasePart = result.Instance
local model = hit:FindFirstAncestorWhichIsA("Model")
local humanoid = model and model:FindFirstChildWhichIsA("Humanoid")
if humanoid then
math.randomseed(tick())
local r = math.random(1,2)
if humanoid.Health > 35 then
script.Parent.Torso.Swing:Play()
script.Parent.Torso.Hit:Play()
animations[string.format("Swing%s",tostring(r))]:Play()
spawn(function() -- "spawn" function creates new thread separate from the the main thread
humanoid:TakeDamage(config.Damage.Value)
end)
break
else
script.Parent.Torso.HeavyHit:Play()
animations[string.format("HeavySwing%s",tostring(r))]:Play()
spawn(function() -- "spawn" function creates new thread from the main thread
humanoid:TakeDamage(50)
end)
break
end
end
end
end
end
In your original code, the “TakeDamage” call is executed immediately in the main thread of the script. This means that the script has to wait for the server to process the damage before it can continue running, which could be causing the delay.
This updated code the “TakeDamage” call is wrapped in a “spawn” function, which creates a new thread to execute the call. This allows the main thread to continue running immediately without waiting for the server to process the damage. The “TakeDamage” call will still be executed but in a separate thread. Should help reduce the delay if this is the issue.