I have made beam effects for my sword so it can spin around the sword using one middle part which is welded to all other parts making up the beams.
I’ve made a code that should be spinning the middle part in a 360 motion repeatedly but it doesn’t work.
Code
local Sword = script.Parent.Parent.Parent.Sword
local Handle = Sword:WaitForChild("Handle")
local Equipped = false
local BeamCurlEffect = nil
local function EquippedSword()
Equipped = true
BeamCurlEffect = ReplicatedStorage:FindFirstChild("BeamCurlEffect"):Clone()
BeamCurlEffect.Parent = workspace
while Equipped do
RunService.RenderStepped:Wait()
if BeamCurlEffect:FindFirstChild("Middle") then
BeamCurlEffect.Middle.CFrame = Handle.CFrame
BeamCurlEffect.Middle.CFrame *= CFrame.fromEulerAnglesXYZ(0,0,0.5)
end
end
end
Sword.Equipped:Connect(EquippedSword)
Sword.Unequipped:Connect(function()
Equipped = false
if BeamCurlEffect then
BeamCurlEffect:Destroy()
end
end)
Note: I’ve also tried putting the beams inside the sword but when I rotate the Middle part it just rotates the entire character.
Are there any ways to fix this or any alternatives I can do to accomplish this? any help is appreciated.
You could place a single anchored part that holds all of the attachments. Without welds.
My way of doing this would be:
local Sword = script.Parent.Parent.Parent.Sword
local Handle = Sword:WaitForChild("Handle")
local Equipped = false
local currentConnection = nil
local function EquippedSword()
Equipped = true
BeamCurlEffect = ReplicatedStorage:FindFirstChild("BeamCurlEffect"):Clone()
BeamCurlEffect.Parent = workspace
currentConnection = RunService.Heartbeat:Connect(function()
-- assuming this is a single part.
if BeamCurlEffect.Parent then
BeamCurlEffect.CFrame = Handle.CFrame * CFrame.Angles(0,0,0.5)
else
currentConnection:Disconnect()
end
end)
end
Sword.Equipped:Connect(EquippedSword)
Sword.Unequipped:Connect(function()
if BeamCurlEffect then
BeamCurlEffect:Destroy()
end
end)
Oh I totally forgot about that. What my friend did is that he created a variable called “rot” which stands for rotation and it added +.5 repeatedly in a RunService loop. Also setting the CFrame of the BeamCurlEffect based on rot.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Sword = script.Parent.Parent.Parent.Sword
local Handle = Sword:WaitForChild("Handle")
local Equipped = false
local currentConnection = nil
local function EquippedSword()
Equipped = true
BeamCurlEffect = ReplicatedStorage:FindFirstChild("BeamCurlEffect"):Clone()
BeamCurlEffect.Parent = workspace
local rot = 0
currentConnection = RunService.Heartbeat:Connect(function()
if BeamCurlEffect.Parent then
rot += 0.5
BeamCurlEffect:SetPrimaryPartCFrame(Handle.CFrame * CFrame.Angles(0,0,rot))
else
currentConnection:Disconnect()
end
end)
end
Sword.Equipped:Connect(EquippedSword)
Sword.Unequipped:Connect(function()
if BeamCurlEffect then
BeamCurlEffect:Destroy()
end
end)