Help With For, I Script

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)
2 Likes
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)

2 Likes

that might work actually work ill try it

2 Likes

I doubt it.

Not very good at scripting.

1 Like

it doesnt work but i might try something like it to get it to work

2 Likes

sooo it works but i had to tweak the script a lil bit

2 Likes

its fine, i can’t believe that worked tho

2 Likes

Well, to be fair… You had to tweak it.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.