I’m making a Thunder Clap, where it’s supposed to knock back everyone in a radius. It works, but it can be buggy, for example sometimes when somebody is in range it wont detect them. And when multiple people are in range, theres a delay before one of them are knocked back. Heres an example: https://gyazo.com/3a9b028c67489c62118daaeb7706b10c
How could I fix this?
Script:
--//Services
local RepStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
--//Instances
local Ragdoll = require(RepStorage.Ragdoll)
local ClapEvent = script.ClapEvent
local Effects = script.Effects
local Sound = script:WaitForChild("Clap")
--//Data
local MaxDistance = 30
local KnockbackForce = 80000
local KnockbackVelocity = 200
local Damage = 30
local RagTime = 1.5
--//Functions
--//Events
--//Script
ClapEvent.OnServerEvent:Connect(function(Player)
local Particles = {
Big1 = Effects.Big1:Clone(),
Shockwave1 = Effects.Shockwave1:Clone(),
Shockwave2 = Effects.Shockwave2:Clone(),
SonicBoom = Effects.SonicBoom:Clone()
}
Sound:Play()
local Character = Player.Character
local Hand = Character:WaitForChild("RightHand")
local HRP = Character:WaitForChild("HumanoidRootPart")
local UpperTorso = Character:WaitForChild("UpperTorso")
local HandAttach = Instance.new("Attachment", UpperTorso)
HandAttach.Name = "BoomAttachment"
HandAttach.CFrame = CFrame.new(0, 0, -2)
Particles.Big1.Parent = HandAttach
Particles.Shockwave1.Parent = HandAttach
Particles.Shockwave2.Parent = HandAttach
Particles.SonicBoom.Parent = HandAttach
Particles.Big1.Enabled = true
Particles.Shockwave1.Enabled = true
Particles.Shockwave2.Enabled = true
Particles.SonicBoom.Enabled = true
Debris:AddItem(HandAttach, 2.3)
Debris:AddItem(Particles.Big1, 0.7)
Debris:AddItem(Particles.Shockwave1, 1)
Debris:AddItem(Particles.Shockwave2, 1.2)
Debris:AddItem(Particles.SonicBoom, 1.5)
for _,plr in ipairs(game.Players:GetPlayers()) do
if plr ~= Player then
if Player:DistanceFromCharacter(plr.Parent.HumanoidRootPart.Position) <= MaxDistance then
local EnemyChar = Player.Character
local EnemyHum = EnemyChar:WaitForChild("Humanoid")
local EnemyHRP = EnemyChar.HumanoidRootPart
print("Found", EnemyHRP)
local EnemyBV = Instance.new("BodyVelocity", EnemyHRP)
EnemyBV.MaxForce = Vector3.new(KnockbackForce, KnockbackForce, KnockbackForce)
EnemyBV.Velocity = EnemyHRP.CFrame.LookVector * -1 * KnockbackVelocity
EnemyBV.P = 10000
EnemyHum:TakeDamage(Damage)
Ragdoll.Ragdoll(EnemyChar)
wait(0.5)
EnemyBV:Destroy()
wait(RagTime)
Ragdoll.UnRagdoll(EnemyChar)
end
end
end
end)
I would have the client perform the clap. Then fire off to the server that’s its being done. The server can then fire a remote event to all clients and each client then performs the knockback. There will still be a bit of delay due to networking but that can’t really be helped. The reason you getting the delay your currently getting is because your looping through each player and the function has wait periods.
If each client is performing their own knockback that will be as close to parallel processing as you could get.
This means the code wrapped in the spawn won’t delay the loop or any code after it.
Note that knockback will always be lagged, because it has to be replicated from the client to the server, then to the client being acted on, then back to the server, then back to the client for visuals.
for _,plr in ipairs(game.Players:GetPlayers()) do
if plr ~= Player then
if Player:DistanceFromCharacter(plr.Parent.HumanoidRootPart.Position) <= MaxDistance then
Helpful advise, but I have no idea how to go about that, I have trouble with client server communication and vice versa, could you give me an example or something on how to go about that?
Ok so here is some example scripts. You’ll probably need to edit them a bit but they’ll give you the general idea. You’ll still need the local script that will actually perform the ThunderClap. That script should handle the effects for the player performing it and fire the event to the server. It would also utilize a part as a zone to detect players within range. But you could use distance detection as well.
ServerScript:
--[[
SERVER SCRIPT
--This server script would listen for claps being performed by players
--It would apply damage, set the acceleration to use for knockbacks
--detect which players are affect and fire a remote to tell affected
--players to perform a knockback.
--]]
--Service to get the player/char information
local Players = game:GetService("Players")
--Create the ThunderClap remote event
local thunderClapEvent = Instance.new("RemoteEvent")
thunderClapEvent.Name = "RemoteTCE"
local accel = game.Workspace.Gravity + 200 --KnockBackVelocity
local dmgAmount = 30
--Listen for player's peforming a ThunderClap
local tceConn = nil
tceConn = thunderClapEvent.OnServerEvent:Connect(function(player)
--Get the clapZone associated with this player
local clapZone = player.Character:FindFirstChild("ClapZone")
local clapZoneConn = nil
clapZoneConn = clapZone.Touched:Connect(function() return end)--Add touch interest
--Get list of enemies touching the zone
local parts = clapZone:GetTouchingParts()
for i = 1, #parts do
--Verify its a player character and get player
if(parts[i].Parent)then
local humanoid = parts[i].Parent:FindFirstChildWhichIsA("Humanoid")
if(humanoid)then
local enemyChar = humanoid.Parent
--APPLY DAMAGE?
humanoid:TakeDamage(dmgAmount)
local enemyPlayer = Players:GetPlayerFromCharacter(enemyChar)
if(enemyPlayer)then
--Tell the player client to perform knockback move
--Provide the acceleration to use
thunderClapEvent:FireClient(enemyPlayer, accel)
end
end
end
end
end)
Client KnockBack Script:
--[[
CLIENT SCRIPT
--This script would respond to the server firing a thunderclap event
--inwhich this player is affected and perform the knockback of this player's character
--A different client script would be required to perform the clap
]]--
local RepStor = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local char = player.Character or player.Character.Added:Wait()
local humanoid = char:WaitForChild("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")
--Get the character's total mass
local mass = 0
local charParts = char:GetDescendants()
for i = 1, #charParts do
if(charParts[i]:IsA("BasePart"))then
mass += charParts[i].Mass
end
end
--//Instances
local Ragdoll = require(RepStor.Ragdoll)
--//Data
local MaxDistance = 30
local KnockbackForce = 80000
local KnockbackVelocity = 200
local Damage = 30
local RagTime = 1.5
--Event to handle thunderclaps
local thunderClapEvent = nil
local tceConn = nil --will hold the connection to the event
--Function to handle thunderclap knockbacks
local function HandleThunderClap(accel)
--You could replace the bodyvelocity object with ApplyImpulse..
local dir = (hrp.CFrame.LookVector * -1) + Vector3.new(0,1,0) --Backward and Up
local force = dir * mass * accel
Ragdoll.Ragdoll(char)
hrp:ApplyImpulse(force)
task.Wait(RagTime)
Ragdoll.UnRagdoll(char)
end
--Monitor instances added to rep storage until we get the thunderClap event
local repConn = nil
local repConn = RepStor.ChildAdded:Connect(function(child)
if(child == "RemoteTCE" and child:IsA("RemoteEvent"))then
--Store ref to this remote event instance
thunderClapEvent = child
--Listen for the event to be fired by the server.
tceConn = thunderClapEvent.OnClientEvent:Connect(HandleThunderClap)
--Disconnect now that we have the event
if(repConn)then
repConn:Disconnect()
repConn = nil
end
end
end)