Fireball system

repeat wait() until game:IsLoaded() 

local player = game.Players.LocalPlayer

repeat wait() until player.CharacterAppearanceLoaded
local Tool = script.Parent
local RS = game:GetService("ReplicatedStorage")
local Functions = RS:WaitForChild("Functions")
local char = player.Character
local UIS = game:GetService("UserInputService")
local FireRemotes = Functions:WaitForChild("Fire")
local FireballEvent = FireRemotes:WaitForChild("Fireball")
local mouse = player:GetMouse()
local hmrp = char:WaitForChild("HumanoidRootPart")
local anims = RS.Anims
local fx = RS:WaitForChild("Clones").Fire
local ts = game:GetService("TweenService")
local animLength = 0.4

local function rayCast(startPosition, endPosition, filter, length, obj)

	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.IgnoreWater = true
	params.FilterDescendantsInstances = filter

	local result = workspace:Raycast(obj.Position, (endPosition - startPosition).Unit * length,params)

	if result then 
		--obj.Position = result.Position
		
		return false
	else 
	--	obj.Position = (endPosition - startPosition).Unit * length + startPosition
		return true 		
	end

end

local function fireCharge(data)
	local c = data.c
	local h = c.Humanoid
	local root = c.HumanoidRootPart
	local angle = 0 


	for i = 1, 5 do 
		angle += 360/5
		local fireball = fx.Fireball:Clone()
		fireball.Parent = workspace.BulletFolder
		fireball.CFrame = root.CFrame * CFrame.fromEulerAnglesXYZ(0, math.rad(angle), 0) * CFrame.new(15, 5, 0)
		game.Debris:AddItem(fireball,animLength)

	end



	local bodyPos = Instance.new("BodyPosition",root)
	bodyPos.Position = root.Position
	bodyPos.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	bodyPos.P = 1200
	bodyPos.D = 400
	h.JumpHeight = 0

	local fireEffect = fx.Parent.Parent.Clones.Particles.Fire:Clone()
	fireEffect.Parent = root




	wait(animLength)

	fireEffect:Destroy()
	--handFire:Destroy()

	h.JumpHeight = 7.2
	bodyPos:Destroy()



end

local function fireBall(data)


	local c = data.c
	local h =c.Humanoid
	local root = c.HumanoidRootPart 

	local angle = 0 

	for i=1,5 do 

		angle += 360/5

		local fireball = fx.Fireball:Clone()
		game.Debris:AddItem(fireball,1)
		fireball.Parent = game.Workspace.BulletFolder
		fireball.CFrame = root.CFrame * CFrame.fromEulerAnglesXYZ(0, math.rad(angle), 0) * CFrame.new(15, 5, 0)


		fireball.Attatch.Fire.Enabled = true


		local connection
		local count = 0

		connection = game:GetService("RunService").RenderStepped:Connect(function(dt)

			
			if 	rayCast(fireball.Position, data.endpoint, {c,workspace.BulletFolder}, 100 * dt, fireball ) == false then


				for i = 0, 15 do 


					local part = Instance.new("Part",workspace.BulletFolder)
					part.Anchored = true
					part.CFrame = CFrame.new(data.endpoint) * CFrame.Angles(0,math.rad(i*24),0) * CFrame.new(0,0,-8) * CFrame.Angles(math.rad(35),0,0)
					part.CanQuery = false
					part.CanCollide = false
					part.CanTouch = false

					local params = RaycastParams.new()
					params.FilterType = Enum.RaycastFilterType.Blacklist
					params.IgnoreWater = true
					params.FilterDescendantsInstances = {c,workspace.BulletFolder}

					local result = workspace:Raycast(part.Position + Vector3.new(0,2,0), Vector3.new(0,-10,0),params)
					if result then 
						part.Position = result.Position
						part.Material = result.Material
						part.Color = result.Instance.Color

					end


					local fire = fx.Fireball:Clone() 
					fire.Size = Vector3.new(math.random(10,20)/10, math.random(10,20)/10, math.random(10,20)/10)
					fire.Position = part.Position +Vector3.new(0,3,0)
					fire.Material = part.Material
					fire.Color = part.Color
					fire.Attatch.Fire.Enabled = true
					fire.Attatch.Fire.LockedToPart = false
					fire.Parent = workspace.BulletFolder
					fire.Anchored = false
					fire.CanCollide = true



					local bv = Instance.new("BodyVelocity",fire)
					bv.Velocity = Vector3.new(math.random(-40,40),30,math.random(-40,40))
					bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
					bv.Name = "Velocity"


					delay(1, function()
						fire.Transparency = 1
					end)
					game.Debris:AddItem(bv,.1)
					game.Debris:AddItem(fire,2)




				end

			

				connection:Disconnect()



				if data.client == true then
					FireballEvent:FireServer("FireballHit",data)

				end

				return 
			end
		end)


	end
end

Tool.Equipped:Connect(function()

	UIS.InputBegan:Connect(function(input,Typing)
		if Typing or char.Humanoid.Health ==  0 then return end 

		if input.KeyCode == Enum.KeyCode.F then 
			if mouse.Hit then
				local data = {client = true, c = char}
				local load:AnimationTrack = char.Humanoid:LoadAnimation(anims.Fire.FireballAnim)
				load.Looped = false

				load:Play()
				game.Debris:AddItem(load,0.4) 
				FireballEvent:FireServer("FireCharge", data)
				fireCharge(data)
				data.endpoint = mouse.Hit.Position
				FireballEvent:FireServer("Fireball",data)
				fireBall(data)


			end	
		end
	end)
end)



FireballEvent.OnClientEvent:Connect(function(data)
	if data.Attack == "Fireball" then
		fireBall(data)
	elseif data.Attack == "FireCharge" then
		fireCharge(data)
	end
end)

This is a script i found from a tutorial and then remade to my liking. However, I dont copmletely get it so i was wondering if people can review this code, break it down so i understand it, and explain what i can do better. thanks that would be amazing.

1 Like

Sorry, I know is not related to your question, but I think this should go in #help-and-feedback:scripting-support other than #help-and-feedback:code-review, but sorry, I’m not good at scripting so I can’t really help you :person_shrugging:

mb and its all g, im learning it tho

Is there any particular point that you’d like a deeper understanding of? Or would you like a full explanation of the entire code?

I figured it out after retyping it! Thanks tho :smiley: !