I have a script and everything in it works but i want to make it able to be used for every part that has “Cylinder” in its name. Everything works but one thing, whenever I move to another part the weld always attaches to the first part I used the script on, its hard to explain.
–Here’s The Script
local UIS = game:GetService("UserInputService")
local swinging = false
local weld = Instance.new("Weld")
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")
-- 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
print("check2")
char.PrimaryPart.CFrame = Cylinder.CFrame * CFrame.new(0, 10, 0)
weld.Part0 = Cylinder
weld.Part1 = char.PrimaryPart
weld.Parent = char
weld.C1 = CFrame.new(0, 2, 0)
animTrack:Play()
swinging = true
end
end
else
animTrack:Stop()
weld.Parent = game.ReplicatedStorage
task.wait(0.1)
swinging = false
end
end
game.ReplicatedStorage.SwingEvent.OnServerEvent:Connect(onSwingEvent)
local UIS = game:GetService("UserInputService")
local swinging = false
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")
-- 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
print("check2")
char.PrimaryPart.CFrame = Cylinder.CFrame * CFrame.new(0, 10, 0)
-- Create a new weld instance for each part
local weld = Instance.new("Weld")
weld.Part0 = Cylinder
weld.Part1 = char.PrimaryPart
weld.Parent = char
weld.C1 = CFrame.new(0, 2, 0)
animTrack:Play()
swinging = true
end
end
else
animTrack:Stop()
-- Remove the weld from the character
for _, weld in pairs(char:GetChildren()) do
if weld:IsA("Weld") then
weld:Remove()
end
end
task.wait(0.1)
swinging = false
end
end
game.ReplicatedStorage.SwingEvent.OnServerEvent:Connect(onSwingEvent)