I only have one script in each of my swords and this is it, as you can see it’s very messy and quite buggy sometimes. I was wondering if any of you guys have any tips to optimize this code and maybe make it less buggy as well. Thanks!
Please provide the interactable text instead of images; (Send the code here so i can analyze and possibly send an optimized and organized version)
ok I’ll send it now:
--Idle Animation
local anim = tool.Idle
local track
tool.Equipped:Connect(function()
track = tool.Parent.Humanoid:LoadAnimation(anim)
track.Priority = Enum.AnimationPriority.Action
track:Play()
end)
tool.Unequipped:Connect(function()
if track then
track:Stop()
end
end)
tool.Destroying:Connect(function()
if track then
track:Stop()
end
end)
--Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ds = game:GetService("Debris")
local mps = game:GetService("MarketplaceService")
--CONFIGURATION
local Turn = 1
local Cooldown = 0.03
local dmg = tool.dmg
local Price = tool.Price
local swing1sound = tool.Handle.Swing1Sound
local swing2sound = tool.Handle.Swing2Sound
local hitsound = tool.Handle.HitSound
local strength = tool.knockbackstrength.Value
local deadcheck = false
--Effects
local bloodeffect = ReplicatedStorage:WaitForChild("SwordEffects"):WaitForChild("BloodParticles")
local sparkeffect = ReplicatedStorage:WaitForChild("SwordEffects"):WaitForChild("SparksParticles")
local hiteffect = ReplicatedStorage:WaitForChild("SwordEffects"):WaitForChild("HitEffects")
--END
tool.Activated:Connect(function()
if tool.Cooldown.Value == false then
tool.Cooldown.Value = true
local Humanoid = tool.Parent.Humanoid
local Anim1 = Humanoid:LoadAnimation(tool.Attack1)
local Anim2 = Humanoid:LoadAnimation(tool.Attack2)
if track then
track:Stop()
end
if Turn == 1 then
Anim1:Play()
wait(0.1)
tool.CanDamage.Value = true
swing1sound:Play()
Turn = 2
Anim1.Stopped:wait(Cooldown)
tool.CanDamage.Value = false
tool.Cooldown.Value = false
elseif Turn == 2 then
Anim2:Play()
wait(0.1)
tool.CanDamage.Value = true
swing2sound:Play()
Turn = 1
Anim2.Stopped:Wait(Cooldown)
tool.CanDamage.Value = false
tool.Cooldown.Value = false
end
wait()
track:Play()
else
return
end
end)
tool.Handle.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
local Humanoid = Hit.Parent.Humanoid
local char = Hit.Parent
if tool.CanDamage.Value == true and Humanoid.Health >= 0 then
local dead = false
local HumanoidRP = Hit.Parent:FindFirstChild("HumanoidRootPart")
local UpperTorso = Hit.Parent:FindFirstChild("UpperTorso")
local plr = game:GetService("Players"):GetPlayerFromCharacter(tool.Parent)
--gamepass x2 money
local userId = plr.UserId
local gamepass = 85869838
--Clone effects
local effectsclone = hiteffect:Clone()
local bloodeffectclone = bloodeffect:Clone()
local sparkeffectclone = sparkeffect:Clone()
effectsclone.Parent = UpperTorso
local weldeffects1 = Instance.new("Weld", effectsclone)
weldeffects1.Part0 = UpperTorso
weldeffects1.Part1 = effectsclone
bloodeffectclone.Parent = UpperTorso
local weldeffects2 = Instance.new("Weld", bloodeffectclone)
weldeffects2.Part0 = UpperTorso
weldeffects2.Part1 = bloodeffectclone
sparkeffectclone.Parent = UpperTorso
local weldeffects3 = Instance.new("Weld", sparkeffectclone)
weldeffects3.Part0 = UpperTorso
weldeffects3.Part1 = sparkeffectclone
--knockback
local bv = Instance.new("BodyVelocity")
bv.P = 2500
bv.MaxForce = Vector3.new(100000,100000,100000)
bv.Velocity = CFrame.new(tool.Parent:FindFirstChild("HumanoidRootPart").Position, char:FindFirstChild("HumanoidRootPart").Position).lookVector * (strength/2)
bv.Parent = char:FindFirstChild("HumanoidRootPart")
ds:AddItem(bv, 0.2)
Humanoid:TakeDamage(dmg.Value)
local findcombogui = plr.PlayerGui:FindFirstChild('ComboGui')
if findcombogui then
if mps:UserOwnsGamePassAsync(userId, gamepass) then
plr.PrivateStats.Money.Value += 2
else
plr.PrivateStats.Money.Value += 1
end
Humanoid.Died:Connect(function()
if deadcheck == false then
deadcheck = true
plr.leaderstats.Kills.Value += 1
if mps:UserOwnsGamePassAsync(userId, gamepass) then
plr.PrivateStats.Money.Value += 10
else
plr.PrivateStats.Money.Value += 5
end
end
end)
findcombogui.Combo.Value = findcombogui.Combo.Value + 1
findcombogui.ShowCombo.Despawn.Disabled = true
findcombogui.ShowCombo.Despawn.Disabled = false
else
local comboGui = game:GetService("ServerStorage").ComboGui:Clone()
comboGui.Parent = plr.PlayerGui
end
hitsound:Play()
tool.CanDamage.Value = false
else
return
end
else
return
end
deadcheck = false
end)
ummmm I don’t know why it came out like this
You can type ```
And then simply copy paste the code, and then end with 3x ` again
-- It'll become a codeblock
print("see?")
check the first reply I edited it
You’re using a script. You should be using a local script. That does mean you’ll need to add some remote events, to properly ensure replication.
Here’s what you do:
On a script, in ServerScriptService
local attackRemote = Instance.new("RemoteEvent",game.ReplicatedStorage)
attackRemote.Name = "attackRemote"
local damageRemote = Instance.new("RemoteEvent",game.ReplicatedStorage)
damageRemote.Name = "damageRemote"
function swordAttack(player)
-- Here you add things like, play audio
end
function DamagePlayer(player, target)
-- Here you damage, apply knockback, etc.
end
attackRemote.OnServerEvent:Connect(swordAttack)
damageRemote.OnServerEvent:Connect(damageRemote)
On the client, you’ll do
tool.Activated:Connect(function()
game.ReplicatedStorage.attackRemote:FireServer()
end)
function hitPlayer(target)
game.ReplicatedStorage.damageRemote:FireServer(target)
end)
It’d be best to also calculate if a hit was made on the server.
I see, although I don’t fully understand why I can’t use a normal server script.
The logic doesn’t need to be ran on the server. If you want to optimize, you shouldn’t run things on the server if you don’t have to. It’s also detrimental to latency. For instance, if damaging someone is done by the server, it’s OK (it might take 0.1s), but if using the sword is controlled by the server it will feel really bad because of the delays. There’s no delay when the client is controlling it.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.