When a player hits an npc/mob with my sword the whole server starts lagging for about 1s even the the sword hit script is local its causing a global lag and I am confused
Would be helpful if we could see some code please, even if it’s local sided you must be firing an event of some sort?
repeat wait() until script.Parent:FindFirstChild("WeaponConfig") ~= nil
local _M = require(script.Parent.WeaponConfig)
local debounce = false
Tool = script.Parent
Handle = Tool:WaitForChild("Handle")
valueCooldown = false
local SlashSound = Instance.new("Sound")
SlashSound.SoundId = "rbxasset://sounds\\swordslash.wav"
SlashSound.Parent = Handle
SlashSound.Volume = .5
SlashSound.MaxDistance = 50
function SwordCore(hit)
if script.CanDamage.Value and hit.Parent:FindFirstChild(_M.HumanoidToKill) then
local enemy = hit.Parent:FindFirstChild(_M.HumanoidToKill)
local chr = Tool.Parent
local plr = game.Players:GetPlayerFromCharacter(chr)
if (not plr) then return end
local hum = chr:FindFirstChild("Humanoid")
if (not hum) then return end
if enemy ~= hum and (not debounce) then
if (enemy.Health < 1) then return end
if chr:FindFirstChild("Right Arm") then
debounce = true
local right_arm = chr:FindFirstChild("Right Arm")
local j = right_arm:FindFirstChild("RightGrip")
if (j and (j.Part0 == Handle or j.Part1 == Handle)) then
if (not enemy) then return end
game.ReplicatedStorage.GameRemotes.DamageEvent:FireServer(hit,enemy,Tool)
end
wait(_M.Cooldown)
debounce = false
end
end
end
end
function onActivated()
if (not Tool.Enabled) then return end
Tool.Enabled = false
local chr = Tool.Parent
local hum = chr:FindFirstChild("Humanoid")
local anim = hum:LoadAnimation(script.Parent.Slash)
if (hum == nil) then return end
SlashSound:Play()
anim:Play()
script.Parent.Unequipped:Connect(function()
anim:Stop()
end)
wait(_M.Cooldown)
Tool.Enabled = true
end
function onEquipped()
if (not Tool) then return end
if (not Handle) then return end
if Handle.UnsheathSound.IsPlaying == false then
Handle.UnsheathSound:Play()
end
end
function onUnequipped()
if (not Tool) then return end
if (not Handle) then return end
Handle.UnsheathSound:Stop()
end
function checkValue()
if valueCooldown == false then
valueCooldown = true
script.CanDamage.Value = true
wait(_M.Cooldown)
wait()
script.CanDamage.Value = false
valueCooldown = false
end
end
script.Parent.Activated:Connect(onActivated)
script.Parent.Activated:Connect(checkValue)
script.Parent.Equipped:Connect(onEquipped)
script.Parent.Unequipped:Connect(onUnequipped)
connection = Handle.Touched:Connect(SwordCore)
local _C = {
--] Basic Configuration
["MaxDamage"] = 2;
["MinDamage"] = 1;
["Cooldown"] = 1;
["Cost"] = 0;
--] Advanced
["HumanoidToKill"] = ("Enemy")
}
return _C
Could I see the server side too and where this remote is fired?
game.ReplicatedStorage.GameRemotes.DamageEvent:FireServer(hit,enemy,Tool)
No, the server script so where it says .OnServerEvent please.
Sorry, but I dont really know what you mean
The script that contains the OnServerEvent/the code for the DamageEvent
this?
That’s the Module script, check GameRemoteHandler
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local RemoteFolder = ReplicatedStorage:WaitForChild(“GameRemotes”)
local ItemFolder = ReplicatedStorage:WaitForChild(“GameItems”)
game.Players.PlayerAdded:Connect(function(plr)
wait()
local BindableEvent = Instance.new(“RemoteEvent”,plr)
BindableEvent.Name = “ShopBind”
end)
RemoteFolder:WaitForChild(“BuyEvent”).OnServerEvent:Connect(function(plr,item)
local lstats = plr:WaitForChild(“leaderstats”)
local stat = lstats:WaitForChild(“Gold”)
local BP = plr:WaitForChild(“Backpack”)
local SG = plr:WaitForChild(“StarterGear”)
if (plr) and (item ~= nil) then
local LoadedGear = ItemFolder:WaitForChild(item)
if stat.Value >= require(LoadedGear.WeaponConfig).Cost then
if BP:FindFirstChild(item) or SG:FindFirstChild(item) then
require(script.NotifyEvent).Notify(plr,“You already own “…item…”.”)
return
end
require(script.BuyEvent).Purchase(plr,item,require(LoadedGear.WeaponConfig).Cost)
require(script.NotifyEvent).Notify(plr,“You purchased the “…item…” for “…require(LoadedGear.WeaponConfig).Cost…” gold.”)
else
require(script.NotifyEvent).Notify(plr,“You don’t have enough gold for “…item…”.”)
end
end
end)
RemoteFolder:WaitForChild(“SellEvent”).OnServerEvent:Connect(function(plr,item)
local lstats = plr:WaitForChild(“leaderstats”)
local stat = lstats:WaitForChild(“Gold”)
local BP = plr:WaitForChild(“Backpack”)
local SG = plr:WaitForChild(“StarterGear”)
if (plr) and (item ~= nil) then
local BPG = BP:FindFirstChild(item)
local SGG = SG:FindFirstChild(item)
local LoadedGear = ItemFolder:WaitForChild(item)
if (BPG~=nil) and (SGG~=nil) then
require(script.SellEvent).Sell(plr,item,require(LoadedGear.WeaponConfig).Cost)
require(script.NotifyEvent).Notify(plr,“You sold your “…item…” for “…require(LoadedGear.WeaponConfig).Cost…” gold.”)
else
require(script.NotifyEvent).Notify(plr,“You don’t own the “…item…”.”)
end
end
end)
RemoteFolder:WaitForChild(“DamageEvent”).OnServerEvent:Connect(function(plr,hit,enemy)
require(script.DamageEvent).Damage(plr,hit,enemy)
end)
RemoteFolder:WaitForChild(“NotifyEvent”).OnServerEvent:Connect(function(plr,msg)
require(script.NotifyEvent).Notify(plr,msg)
end)
RemoteFolder:WaitForChild(“CloseGUIEvent”).OnServerEvent:Connect(function(plr,gui,action)
if action == “Visible” then gui.Visible = (not gui.Visible) end
if action == “Destroy” then gui:Destroy() end
end)
Okay now it erquires the DamageEvent Module, what’s in there?
_Module = {}
function CreateTag(plr,enemy)
if enemy ~= nil then
if (enemy:FindFirstChild(“Player_Tag”)) then return end
local tag = Instance.new(“ObjectValue”, enemy)
tag.Name = “Player_Tag”
tag.Value = plr
end
end
function DestroyTag(enemy)
if enemy ~= nil then
local tag = enemy:FindFirstChild(“Player_Tag”)
if tag ~= nil then tag:Destroy() end
end
end
local function CheckDistance(plr,tor,dist)
if (not tor) or (not tor.Parent) or (not tor.Parent:FindFirstChild(“MobConfig”)) then return end
if plr:DistanceFromCharacter(Vector3.new(tor.Position.X,tor.Position.Y,tor.Position.Z)) < require(tor.Parent.MobConfig).FollowDistance then
return true else return false
end
end
function addComas(str)
return #str % 3 == 0 and str:reverse():gsub(“(%d%d%d)”, “%1,”):reverse():sub(2) or str:reverse():gsub(“(%d%d%d)”, “%1,”):reverse()
end
local function CreateObject(hit,dmg)
local ObjModel = Instance.new(“Model”, workspace.DebrisHolder)
local Obj = Instance.new(“Part”, ObjModel)
– Appearance
Obj.BrickColor = BrickColor.new(“Bright red”)
Obj.Material = Enum.Material.Neon
Obj.Transparency = 1
– Data
Obj.Position = hit.Parent:FindFirstChild(“Head”).Position + Vector3.new(math.random(-1.6,1.6),2,math.random(-1.6,1.6))
– Behavior
Obj.Anchored = true
Obj.CanCollide = false
Obj.Locked = true
– Part
Obj.Shape = Enum.PartType.Ball
Obj.Size = Vector3.new(0.75,0.75,0.75)
– Billboard Gui
local Gui = script.BillboardGui:Clone()
Gui.Adornee = ObjModel:FindFirstChild(“Part”)
Gui.TextLabel.Text = addComas(tostring(dmg))…" damage"
Gui.Parent = ObjModel
local ObjPos = Instance.new("BodyPosition", Obj)
ObjPos.Position = Vector3.new(0,7.85,0)
for i = 1,0,-0.1 do
Obj.Transparency = i
Gui.TextLabel.TextTransparency = i
Gui.TextLabel.TextStrokeTransparency = i
wait()
end
wait(0.50)
for i = 0,1,0.1 do
Obj.Transparency = i
Gui.TextLabel.TextTransparency = i
Gui.TextLabel.TextStrokeTransparency = i
wait()
end
return ObjModel
end
function _Module.Damage(plr,hit,enemy)
local chr = plr.Character or plr.CharacterAdded:Wait()
local tool = chr:FindFirstChildOfClass(“Tool”)
if (not enemy) or (not hit) or (not tool) then return end
local tor = hit.Parent:FindFirstChild(“Torso”)
if (not hit.Parent) or (not tor) then return end
if (game.Players:GetPlayerFromCharacter(enemy.Parent)) then return end
if (not CheckDistance(plr,hit.Parent:FindFirstChild(“Torso”))) then return end
local dmg = math.random(require(tool.WeaponConfig).MinDamage,require(tool.WeaponConfig).MaxDamage)
if (plr:FindFirstChild(“leaderstats”)) then
if plr:FindFirstChild(“Level”) then
dmg = dmg + (plr.leaderstats.Level.Value - 1)
end
end
CreateTag(plr,enemy)
enemy:TakeDamage(dmg)
local ObjModel = CreateObject(hit,dmg)
ObjModel:Destroy()
DestroyTag(ObjModel)
end
return _Module
I think it may be that it’s do a lot of heavy work in the Damage module
Maybe replace the function _Module.Damage function to this?
function _Module.Damage(plr,hit,enemy)
if not enemy or not hit then return end
local chr = plr.Character or plr.CharacterAdded:Wait()
local tool = chr:FindFirstChildOfClass(“Tool”)
if not tool then return end
local tor = hit.Parent:FindFirstChild(“Torso”)
if not tor then return end
if game.Players:GetPlayerFromCharacter(enemy.Parent) then return end
if not CheckDistance(plr,hit.Parent:FindFirstChild(“Torso”)) then return end
local config = require(tool.WeaponConfig)
local dmg = math.random(config.MinDamage,config.MaxDamage)
if (plr:FindFirstChild(“leaderstats”)) then
dmg = dmg + (plr.leaderstats.Level.Value - 1)
end
CreateTag(plr,enemy)
enemy:TakeDamage(dmg)
local ObjModel = CreateObject(hit,dmg)
ObjModel:Destroy()
DestroyTag(ObjModel)
end
Thanks! Works without any lag
That’s good! I knew something was up because it was requiring WeaponConfig twice, which puts strain, and there was some other things too, but that was the biggest thing I saw that contributed.
If you have anymore issues don’t be afraid to make another post!
so actually there is a problem I thought its just bc you put `` instead of ‘’ well I fixed it but the damage is still not working
function _Module.Damage(plr,hit,enemy)
if not enemy or not hit then return end
local chr = plr.Character or plr.CharacterAdded:Wait()
local tool = chr:FindFirstChildOfClass(‘Torso’)
if not tool then return end
local tor = hit.Parent:FindFirstChild(‘Torso’)
if not tor then return end
if game.Players:GetPlayerFromCharacter(enemy.Parent) then return end
if not CheckDistance(plr,hit.Parent:FindFirstChild(‘Torso’)) then return end
local config = require(tool.WeaponConfig)
local dmg = math.random(config.MinDamage,config.MaxDamage)
if (plr:FindFirstChild(‘leaderstats’)) then
dmg = dmg + (plr.leaderstats.Level.Value - 1)
end
CreateTag(plr,enemy)
enemy:TakeDamage(dmg)
local ObjModel = CreateObject(hit,dmg)
ObjModel:Destroy()
DestroyTag(ObjModel)
end
Hmm, try this?
function _Module.Damage(plr,hit,enemy)
local chr = plr.Character or plr.CharacterAdded:Wait()
local tool = chr:FindFirstChildOfClass("Tool")
if (not enemy) or (not hit) or (not tool) then return end
local tor = hit.Parent:FindFirstChild("Torso")
if (not hit.Parent) or (not tor) then return end
if (game.Players:GetPlayerFromCharacter(enemy.Parent)) then return end
if (not CheckDistance(plr,hit.Parent:FindFirstChild("Torso"))) then return end
local config = require(tool.WeaponConfig)
local dmg = math.random(config.MinDamage,config.MaxDamage)
if (plr:FindFirstChild("leaderstats")) then
if plr:FindFirstChild("Level") then
dmg = dmg + (plr.leaderstats.Level.Value - 1)
end
end
CreateTag(plr,enemy)
enemy:TakeDamage(dmg)
local ObjModel = CreateObject(hit,dmg)
ObjModel:Destroy()
DestroyTag(ObjModel)
end
Nope, it does not work.