I have two scripts to make a sword swing with animations and then and make the tool go to the player’s back when unequipped.
I am trying to make a system so I can just make two scripts for all the tools. I can do this by using CollectionService
.
Problem: I don’t know how to go about doing this. I have look and can’t find what I am looking for. I know how to use CollectionService
but I don’t know how to do it for this system. The tools are in replicated storage and get cloned to the players backpack when they equip it from their inventory.
How would I go about doing this?
Server script inside tool
local function onActivated(Hit)
script.Disabled = true
workspace:WaitForChild("BladeBall").MainScript.Disabled = false
wait(0.35)
script.Disabled = false
workspace:WaitForChild("BladeBall").MainScript.Disabled = true
end
script.Parent.Activated:Connect(onActivated)
local Tool = script.Parent
local Character = Tool.Parent.Parent.Character or Tool.Parent.Parent.CharacterAdded:Wait()
local Torso = Character:FindFirstChild("Torso") or Character:WaitForChild("Torso")
Tool.Equipped:Connect(function()
for Index, Child in pairs(Torso:GetChildren()) do
if Child:IsA("Model") and Child.Name == "ToolBackModel" then
Child:Destroy()
end
if Child:IsA("Weld") and Child.Name == "ToolBackWeld" then
Child:Destroy()
end
end
end)
Tool.Unequipped:Connect(function()
local ToolClone = Tool:Clone()
local ToolBackModel = Instance.new("Model")
ToolBackModel.Parent = Torso
ToolBackModel.Name = "ToolBackModel"
for Index, Child in pairs(ToolClone:GetChildren()) do
if Child:IsA("BasePart") or Child:IsA("MeshPart") or Child:IsA("UnionOperation") then
Child.Parent = ToolBackModel
Child.CanCollide = false
Child.Massless = true
end
end
ToolBackModel.PrimaryPart = ToolBackModel:FindFirstChild("Handle")
local ToolBackWeld = Instance.new("Weld")
ToolBackWeld.Name = "ToolBackWeld"
ToolBackWeld.Parent = Torso
ToolBackWeld.Part0 = Torso
ToolBackWeld.Part1 = ToolBackModel:FindFirstChild("Handle")
ToolBackWeld.C1 = CFrame.new(0, 2, -0.5) * CFrame.Angles(0, 0, math.rad(135)) -- Be sure to use math.rad() for CFrame.Angles() since it uses radians not degrees
end)
Local script inside tool's handle
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent -- assuming the LocalScript is inside the sword tool
local animations = {
"rbxassetid://15388446655",
"rbxassetid://15388455036",
"rbxassetid://15388460473",
"rbxassetid://15402133821",
"rbxassetid://15402137447"
}
local cooldownTime = 0.5 -- Set the cooldown time in seconds
local lastAnimationTime = 0
local function playRandomAnimation()
local currentTime = tick()
if currentTime - lastAnimationTime >= cooldownTime then
local randomIndex = math.random(1, #animations)
local animationId = animations[randomIndex]
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
local animation = Instance.new("Animation")
animation.AnimationId = animationId
local animationTrack = humanoid:LoadAnimation(animation)
animationTrack:Play()
local sound = tool.Slash
sound:Play()
end
lastAnimationTime = currentTime
end
end
local function onBladeBallTouched()
local particleEmitter = tool.Attachment.Particle
particleEmitter:Emit(1)
end
mouse.Button1Down:Connect(function()
tool.Touched:Connect(function(hit)
if hit.Name == "BladeBall" then
onBladeBallTouched()
end
end)
playRandomAnimation()
end)