i have this script that works perfectly fine but looping through the workspace can make the game a bit laggy and so someone said to use spatial query api instead of a loop through the workspace, how can i implement spatial query api into this script?
local m2Event = game.ReplicatedStorage.M2Remote.M2Fire
m2Event.OnServerEvent:Connect(function(plr)
local char = plr.Character
local hb = char:FindFirstChild("Torso")
local root = char:FindFirstChild("HumanoidRootPart")
local ws = game.Workspace.PlayersAndMobs
local hum = char:WaitForChild("Humanoid")
hum.WalkSpeed = 3.5
for i,v in pairs(ws:GetChildren()) do -- look through all the workspace
if v:FindFirstChild("Humanoid") and v:FindFirstChild("Head") and v.Name ~= plr.Name and char:FindFirstChild("Blocking") == nil then
local dist = (v.Head.Position - hb.Position).magnitude --find the distance between us and the ting
if dist < 5 then -- if its less than 35 studs then
if v:FindFirstChild("Parry").Value == true then
print("parried")
char:WaitForChild("Humanoid").WalkSpeed = 0
wait(2.3)
char:WaitForChild("Humanoid").WalkSpeed = 16
else
v.Humanoid:TakeDamage(9) -- take 30 damage
script.Parent.HitSound:Play()
local fxcln = script.fx:Clone()
fxcln.Parent = v.Head.Parent.HumanoidRootPart
fxcln.CFrame = v.Head.Parent.HumanoidRootPart.CFrame
game.Debris:AddItem(fxcln,.3)
local bv = Instance.new("BodyVelocity",v.HumanoidRootPart) --make the knockback
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge) -- maximum force
bv.Velocity = root.CFrame.lookVector * 45 + Vector3.new(0,7,10) -- the force
game.Debris:AddItem(bv,.3) --it will last 0.5 seconds
wait(1)
hum.WalkSpeed = 16
end
end
end
end
end)