Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I add animations to a raycast script?

I wrote a raycast script and I would like to add a idle and fire animation

local tool = script.Parent local player = game:GetService("Players").LocalPlayer

tool.Equipped:connect(function(mouse) print("Tool equipped!")

mouse.Button1Down:connect(function()
	print("Mouse pressed!")
	local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
	local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

	local beam = Instance.new("Part", workspace)
	beam.BrickColor = BrickColor.new("Bright red")
	beam.FormFactor = "Custom"
	beam.Material = "Neon"
	beam.Transparency = 0.25
	beam.Anchored = true
	beam.Locked = true
	beam.CanCollide = false

	local distance = (tool.Handle.CFrame.p - position).magnitude
	beam.Size = Vector3.new(0.3, 0.3, distance)
	beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

	game:GetService("Debris"):AddItem(beam, 0.1)

	if part then
		local humanoid = part.Parent:FindFirstChild("Humanoid")

		if not humanoid then
			humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
		end

		if humanoid then
			humanoid:TakeDamage(30)
		end
	end
end)

end)

1 answer

http://wiki.roblox.com/index.php?title=Animations

Everything's explained in the wiki, i'll show you tho.

Now, do not rename the Fire1 to the same name where the variable is.

So the AnimationFire1 is going to be the very first top line

local AnimationFire1 = script.Parent:WaitForChild("AnimationFire1")

To load the animation you do

local Fire1 = Humanoid:LoadAnimation(AnimationFire1)

local NameYourAnimation = script.Parent:WaitForChild("YourAnimationHere")
local tool = script.Parent
local Humanoid = tool.Parent
--Variables
mouse.Button1Down:connect(function()
    print("Mouse pressed!")
    local Fire1 = Humanoid:LoadAnimation(NameYourAnimation)
    Fire1:Play()
    local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
    local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

    local beam = Instance.new("Part", workspace)
    beam.BrickColor = BrickColor.new("Bright red")
    beam.FormFactor = "Custom"
    beam.Material = "Neon"
    beam.Transparency = 0.25
    beam.Anchored = true
    beam.Locked = true
    beam.CanCollide = false

    local distance = (tool.Handle.CFrame.p - position).magnitude
    beam.Size = Vector3.new(0.3, 0.3, distance)
    beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

    game:GetService("Debris"):AddItem(beam, 0.1)

    if part then
        local humanoid = part.Parent:FindFirstChild("Humanoid")

        if not humanoid then
            humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
        end

        if humanoid then
            humanoid:TakeDamage(30)
        end
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.