Hi, im making an item in which when its equipped, it shoots out 4 fireballs on 4 axis, however, when i equip it, they all shoot fowards then sideways the rest of the time.
video
local Loader = require(game:GetService("ServerStorage"):WaitForChild("ToolPacks"):WaitForChild("WeaponsLoader"))
local MAX_DISTANCE = 300
local ALLOWED_USE_TIME = 10
--changed variable name to be more descriptive
local tool = script.Parent
--MOVED THESE INTO THE EQUIPPED FUNCTION,
--IF NOT THEY ARE NOT IN THE FUNCTION THEY WILL NOT WORK WHEN BEING CLONED INTO A BACKPACK
--local player = tool.Parent.Parent
--local character = player.CharacterAdded:Wait()
--local HumanoidRootPart = character.HumanoidRootPart
function Equipped()
--local player = game.Players:GetPlayerFromCharacter(tool.Parent)
local player = Loader.Players:GetPlayerFromCharacter(tool.Parent)
local character = player.Character
local HumanoidRootPart = character.HumanoidRootPart
Loader.RaceRewardsDomainService:AddOrUpdateXpBonus(player)--called for using the weapon
local clonedFireball = game.ReplicatedStorage.Weapons.Fireball:Clone()
local CF = script.Parent.Part1.CFrame * CFrame.new(0, 0.5, -4)
local clonedFireball2 = game.ReplicatedStorage.Weapons.Fireball:Clone()
local CF2 = script.Parent.part2.CFrame * CFrame.new(0, 0.5, -4)
local clonedFireball3 = game.ReplicatedStorage.Weapons.Fireball:Clone()
local CF3 = script.Parent.part3.CFrame * CFrame.new(0, 0.5, -4)
local clonedFireball4 = game.ReplicatedStorage.Weapons.Fireball:Clone()
local CF4 = script.Parent.part4.CFrame * CFrame.new(0, 0.5, -4)
clonedFireball.Parent = workspace
clonedFireball.Sphere.CFrame = CF
clonedFireball2.Parent = workspace
clonedFireball2.Sphere.CFrame = CF2
clonedFireball3.Parent = workspace
clonedFireball3.Sphere.CFrame = CF3
clonedFireball4.Parent = workspace
clonedFireball4.Sphere.CFrame = CF4
local velocityDirection = (clonedFireball.Sphere.Position - script.Parent.Part1.Position).unit
local velocityDirection1 = (clonedFireball2.Sphere.Position - script.Parent.part2.Position).unit
local velocityDirection2 = (clonedFireball3.Sphere.Position - script.Parent.part3.Position).unit
local velocityDirection3 = (clonedFireball4.Sphere.Position - script.Parent.part4.Position).unit
local Velocity = Vector3.new(velocityDirection.X, 0, velocityDirection.Z)
local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.Velocity = Velocity * 200
BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyVelocity.Parent = clonedFireball.Sphere
local BodyVelocity2 = Instance.new("BodyVelocity")
BodyVelocity2.Velocity = Velocity * 200
BodyVelocity2.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyVelocity2.Parent = clonedFireball2.Sphere
local distanceTraveled = 0
local BodyVelocity3 = Instance.new("BodyVelocity")
BodyVelocity3.Velocity = Velocity * 200
BodyVelocity3.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyVelocity3.Parent = clonedFireball3.Sphere
local distanceTraveled = 0
local BodyVelocity4 = Instance.new("BodyVelocity")
BodyVelocity4.Velocity = Velocity * 200
BodyVelocity4.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyVelocity4.Parent = clonedFireball4.Sphere
local distanceTraveled = 0
clonedFireball.Sphere.Touched:Connect(function(Player)
--assigned humanoid to variable so we do have unnecessary loops in the background
local humanoid = Player.Parent:FindFirstChild("Humanoid")
if humanoid and not Player:IsDescendantOf(player.Character) then
humanoid:TakeDamage(100)
local Explosion = Instance.new("Explosion")
Explosion.Parent = workspace
Explosion.ExplosionType = Enum.ExplosionType.NoCraters
Explosion.BlastRadius = 3
Explosion.DestroyJointRadiusPercent = 0
Explosion.Position = clonedFireball.Sphere.Position
Loader.DebrisService:AddItem(clonedFireball, .1)
local killedPlayer = humanoid.Parent:FindFirstChildOfClass("Player")
if killedPlayer then
--this was failing because you wasnt passing the player, you were passing one of the child parts
--Loader.RaceRewardsDomainService:AddOrUpdateRacePointsBonus(Player, killedPlayer)
Loader.RaceRewardsDomainService:AddOrUpdateRacePointsBonus(player, killedPlayer)--called for a kill
end
elseif not Player.Parent:FindFirstChild("Humanoid") then
end
end)
clonedFireball2.Sphere.Touched:Connect(function(Player)
--assigned humanoid to variable so we do have unnecessary loops in the background
local humanoid = Player.Parent:FindFirstChild("Humanoid")
if humanoid and not Player:IsDescendantOf(player.Character) then
humanoid:TakeDamage(100)
local Explosion = Instance.new("Explosion")
Explosion.Parent = workspace
Explosion.ExplosionType = Enum.ExplosionType.NoCraters
Explosion.BlastRadius = 3
Explosion.DestroyJointRadiusPercent = 0
Explosion.Position = clonedFireball.Sphere.Position
Loader.DebrisService:AddItem(clonedFireball, .1)
local killedPlayer = humanoid.Parent:FindFirstChildOfClass("Player")
if killedPlayer then
--this was failing because you wasnt passing the player, you were passing one of the child parts
--Loader.RaceRewardsDomainService:AddOrUpdateRacePointsBonus(Player, killedPlayer)
Loader.RaceRewardsDomainService:AddOrUpdateRacePointsBonus(player, killedPlayer)--called for a kill
end
elseif not Player.Parent:FindFirstChild("Humanoid") then
end
end)
local previousPosition = clonedFireball.Sphere.Position
local updateConnection
updateConnection = game:GetService("RunService").Stepped:Connect(function()
if not clonedFireball:IsDescendantOf(workspace) then
updateConnection:Disconnect()
return
end
local currentPosition = clonedFireball.Sphere.Position
local stepDistance = (currentPosition - previousPosition).Magnitude
distanceTraveled = distanceTraveled + stepDistance
previousPosition = currentPosition
if distanceTraveled >= MAX_DISTANCE then
--Loader.DebrisService:Destroy() causes this error: The Parent property of Debris is locked
--you wouldn't have seen it because the PurgeTimer() was enabled
--use this to destroy parts instead
Loader.DebrisService:AddItem(clonedFireball, .1)
Loader.DebrisService:AddItem(clonedFireball2, .1)--destroys the item after a defined period of time
Loader.DebrisService:AddItem(clonedFireball3, .1)
Loader.DebrisService:AddItem(clonedFireball4, .1)
end
end)
end
local function PurgeTimer()
wait(ALLOWED_USE_TIME)
local player = Loader.Players:GetPlayerFromCharacter(tool.Parent)
Loader.ToolManagerModule:RemoveAllToolsFromPlayer(player)
end
tool.Equipped:Connect(Equipped)
print("1")
tool.Equipped:Connect(PurgeTimer)