How do I make a simple ranged magic spell?

This kinda works, Somethings wrong with the direction of the spell

local Tool = script.Parent

local Char
local HumRoot



local TweenServ = game:GetService("TweenService")

----Functions

local SpellCastDist = 50
local Speed = 0.3

function LaunchSpell(Port,Dirct,Dmg)
	if Char ~= nil then
		print("SpellShot")
		local Spell = Instance.new("Part",workspace)
		Spell.Size = Vector3.new(1,1,1)
		Spell.Material = Enum.Material.Neon
		Spell.Anchored = true
		Spell.CanCollide = false
		Spell.CFrame = Port.CFrame
		---CreateSpell^^
		local TouchEnabled = nil
		print((Port.Position - Dirct).Magnitude)
		
		local TweenSpell = TweenServ:Create(Spell, TweenInfo.new((Port.Position - Dirct).Magnitude*Speed), {Position = Dirct})
		TweenSpell:Play()
		
		while TouchEnabled == nil do
			wait()
			Spell.Touched:Connect(function(hit)
				if hit and hit.Parent ~= Char and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
					TouchEnabled = false
					hit.Parent:FindFirstChild("Humanoid"):TakeDamage(Dmg)
				end
			end)
		end	
	end
end

Tool.Activated:Connect(function()
	if Char and HumRoot then
		LaunchSpell(HumRoot, HumRoot.CFrame.LookVector*SpellCastDist, 30)
	end
end)

Tool.Equipped:Connect(function()
	Char = Tool.Parent
	HumRoot = Char:WaitForChild("HumanoidRootPart")
end)

Tool.Unequipped:Connect(function()
	Char = nil
	HumRoot = nil
end)

Put this in the script…

I Fixed it

local Tool = script.Parent

local Char
local HumRoot



local TweenServ = game:GetService("TweenService")

----Functions

local SpellCastDist = 50
local Speed = 0.1

function LaunchSpell(Port,Dirct,Dmg)
	if Char ~= nil then
		local Spell = Instance.new("Part",workspace)
		Spell.Size = Vector3.new(1,1,1)
		Spell.Material = Enum.Material.Neon
		Spell.Anchored = true
		Spell.CanCollide = false
		Spell.CFrame = Port.CFrame
		---CreateSpell^^
		local TouchEnabled = nil
		local SpellSpeed = (Port.Position - Dirct).Magnitude*Speed
		
		local TweenSpell = TweenServ:Create(Spell, TweenInfo.new(SpellSpeed), {Position = Dirct})
		TweenSpell:Play()
		
		while TouchEnabled == nil do
			wait()
			Spell.Touched:Connect(function(hit)
				if hit and hit.Parent ~= Char and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
					TouchEnabled = false
					hit.Parent:FindFirstChild("Humanoid"):TakeDamage(Dmg)
				end
			end)
		end	
	end
end

Tool.Activated:Connect(function()
	if Char and HumRoot then
		LaunchSpell(HumRoot, (HumRoot.Position + HumRoot.CFrame.LookVector*SpellCastDist), 30)
	end
end)

Tool.Equipped:Connect(function()
	Char = Tool.Parent
	HumRoot = Tool.Parent:WaitForChild("HumanoidRootPart")
	print(Char)
end)

Tool.Unequipped:Connect(function()
	Char = nil
	HumRoot = nil
end)

image
I think I fixed it

1 Like

Hehe, that’s just what I did, Nice.

1 Like

This script i made doesn’t delete the spell block, but i think you can do that

local Tool = script.Parent

local Char
local HumRoot



local TweenServ = game:GetService("TweenService")

----Functions

local SpellCastDist = 50
local Speed = 0.02

function LaunchSpell(Port,Dirct,Dmg)
	if Char ~= nil then
		local Spell = Instance.new("Part",workspace)
		Spell.Size = Vector3.new(1,1,1)
		Spell.Material = Enum.Material.Neon
		Spell.Anchored = true
		Spell.CanCollide = false
		Spell.CFrame = Port.CFrame
		---CreateSpell^^
		local TouchEnabled = nil
		local SpellSpeed = (Port.Position - Dirct).Magnitude*Speed
		
		local TweenSpell = TweenServ:Create(Spell, TweenInfo.new(SpellSpeed,Enum.EasingStyle.Linear), {Position = Dirct})
		TweenSpell:Play()
		
		TweenSpell.Completed:Connect(function()
			TouchEnabled = false
			Spell:Destroy()
			print("SpellOver")
		end)
		
		while TouchEnabled == nil do
			wait()
			Spell.Touched:Connect(function(hit)
				if hit and hit.Parent ~= Char and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Humanoid").Health > 0 then
					TouchEnabled = false
					print("Touched"..hit.Parent.Name)
					hit.Parent:FindFirstChild("Humanoid"):TakeDamage(Dmg)
					Spell:Destroy()
				end
			end)
		end	
		
	end
end

Tool.Activated:Connect(function()
	if Char and HumRoot then
		LaunchSpell(HumRoot, (HumRoot.Position + HumRoot.CFrame.LookVector*SpellCastDist), 30)
	end
end)

Tool.Equipped:Connect(function()
	Char = Tool.Parent
	HumRoot = Tool.Parent:WaitForChild("HumanoidRootPart")
	print(Char)
end)

Tool.Unequipped:Connect(function()
	Char = nil
	HumRoot = nil
end)