I have a script where everything works… but one thing. Whenever the scripts runs it can be pretty slow and have a delay. Is there anything i can do about it?
–Here’s The Script
–Local Script
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humRoot = char:WaitForChild("HumanoidRootPart")
local Swinging = false
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function (input)
for _, Cylinder in pairs(game.Workspace:GetChildren()) do
if string.match(Cylinder.Name, "Cylinder") then
print("check")
local distance = (char.PrimaryPart.Position - Cylinder.Position).Magnitude
if input.KeyCode == Enum.KeyCode.Space and Swinging == false or input.KeyCode == Enum.KeyCode.ButtonA and Swinging == false then
game.ReplicatedStorage.SwingEvent:FireServer(Swinging)
Swinging = true
else
Swinging = false
end
end
end
end)
–Server Script
local UIS = game:GetService("UserInputService")
local swinging = false
local LastPartName = nil
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://18263356301"
local animTrack -- Declare animTrack outside the function
local function onSwingEvent(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
local humRoot = char:WaitForChild("HumanoidRootPart")
-- Load the animation only if it hasn't been loaded yet
if not animTrack then
animTrack = animator:LoadAnimation(animation)
end
if not swinging then
for _, Cylinder in pairs(game.Workspace:GetChildren()) do
if string.match(Cylinder.Name, "Cylinder") then
local distance = (Cylinder.Position - char.PrimaryPart.Position).Magnitude
if distance < 6 and not Cylinder:FindFirstChild("Weld") and Cylinder.Name ~= LastPartName then
local weld = Instance.new("Weld")
print("check2")
char.PrimaryPart.CFrame = Cylinder.CFrame * CFrame.new(0, 10, 0)
weld.Part1 = Cylinder
weld.Part0 = char.PrimaryPart
weld.Parent = Cylinder
weld.C0 = CFrame.new(0,2,0)
animTrack:Play()
swinging = true
LastPartName = Cylinder.Name
end
end
end
else
for _, Cylinder in pairs(game.Workspace:GetChildren()) do
if string.match(Cylinder.Name, "Cylinder") and (Cylinder.Position - char.PrimaryPart.Position).Magnitude < 4 then
for _, weld in pairs(Cylinder:GetChildren()) do
if weld:IsA("Weld") then
weld:Destroy()
end
end
end
end
task.wait(0.01)
humRoot.AssemblyLinearVelocity = humRoot.AssemblyLinearVelocity * Vector3.new(3,1,3) + Vector3.new(0,32,0)
animTrack:Stop()
swinging = false
task.wait(2)
LastPartName = nil
end
end
game.ReplicatedStorage.SwingEvent.OnServerEvent:Connect(onSwingEvent)
--create using only server script
--update
It also uses a remoteEvent, thought i would just point that out.