I made a script to find the closest player/dummy for a homing fireball. When I activate the fireball, the script does detect the closest player/dummy, but it is unable to return the values for the fireball script.
Here is the local script:
local player = game:GetService("Players").LocalPlayer --get the local player
local char --get the character model
repeat
wait()
char = player.Character
until char
local humanoid = char:FindFirstChild("Humanoid") --get the humanoid
local tool = script.Parent --get the tool
local handle = tool.Handle --get the handle
local flame = handle.Fire --get the "fireball"
local size = flame.Size --get the size of the flame
local mousedown = false --debounce so holding the mouse will not spam and kill the server
local repstorage = game.ReplicatedStorage --get the replicated storage
local shootProjectile = repstorage.shootProjectile --get the remote events
local mouse = player:GetMouse() --get the mouse
function getClosestHumanoidObject() --find the closest humanoid object that is not the local player
local parts = game.Workspace:GetDescendants()
local partPos = {}
for key, Part in pairs(parts) do --loop through the list of children of workspace to filter the list
local plName = player.Name
local plPos = char.HumanoidRootPart.Position
if Part.Parent.Name ~= player.Name then
if Part:IsA("Humanoid") then --check if the part is a basepart/model/union
if Part.Parent:FindFirstChild("HumanoidRootPart") then
local HumRoot = Part.Parent:FindFirstChild("HumanoidRootPart")
if HumRoot then
print("Found the root part of " .. Part.Parent.Name)
end
local dis = (plPos - Part.Parent:FindFirstChild("HumanoidRootPart").Position).magnitude
print("Found the distance between the root part of " .. player.Name .. " and the root part of " .. Part.Parent.Name .. ". The distance is " .. dis)
table.insert(partPos, key, {Part.Name, HumRoot.Position, dis})
end
end
if not Part:IsA("Humanoid") then
end
end
end
local Hunting = partPos[1]
for x = 1,#partPos,1 do
if partPos[x][3] < Hunting[3] then
Hunting = partPos[x]
end
end
if Hunting then
print("\n")
print("Closest humanoid: " .. Hunting[1])
print("Position of humanoid: " .. Hunting[2])
print("Distance from player: " .. Hunting[3])
return Hunting[1],Hunting[2]
else
print("Unable to return values.")
end
end
function loadAnim()
local equipAnim = Instance.new("Animation")
equipAnim.AnimationId = "rbxassetid://4993868195"
local equipTrack = humanoid:LoadAnimation(equipAnim)
local shootAnim = Instance.new("Animation")
shootAnim.AnimationId = "rbxassetid://4993903090"
shootTrack = humanoid:LoadAnimation(shootAnim)
equipTrack:Play()
size = 5
end
function fire()
shootTrack:Play()
local part,position = getClosestHumanoidObject()
shootProjectile:FireServer(handle,part,position)
end
tool.Equipped:Connect(function()
loadAnim()
tool.Deactivated:Connect(function()
mousedown = false
end)
tool.Activated:Connect(function()
mousedown = true
fire()
repeat
wait()
until not mousedown
end)
end)
Here is the server fireball script:
local repstorage = game.ReplicatedStorage
local summonProjectile = repstorage.shootProjectile
function explodetrail(part)
local explosion = Instance.new("Explosion",workspace)
explosion.Position = part.Position
explosion.BlastPressure = 1
explosion.DestroyJointRadiusPercent = 0
explosion.BlastRadius = 10
explosion.ExplosionType = Enum.ExplosionType.NoCraters
explosion.Visible = true
end
summonProjectile.OnServerEvent:Connect(function(player,handle,part,position)
print(part)
local stoptrail = false
local projectile = Instance.new("Part")
projectile.Shape = Enum.PartType.Ball
projectile.Name = player.name .." 's projectile"
projectile.Anchored = false
projectile.Transparency = 0.75
projectile.Size = Vector3.new(0.4,0.4,0.4)
projectile.Parent = workspace
projectile.Material = "Neon"
projectile.BrickColor = BrickColor.new("Neon orange")
projectile.CFrame = CFrame.new(handle.CFrame.p, position)
local attach0 = Instance.new("Attachment",projectile)
attach0.Position = Vector3.new(0.4,0.2,0.2)
local attach1 = Instance.new("Attachment",projectile)
attach1.Position = Vector3.new(0,0.2,0.2)
local startColor = Color3.new(255, 0, 0)
local endColor = Color3.new(255, 165, 0)
local sequence = ColorSequence.new(startColor, endColor)
local tracer = Instance.new("Trail",projectile)
tracer.Attachment0 = attach0
tracer.Attachment1 = attach1
tracer.Color = sequence
local projectilemover = Instance.new("RocketPropulsion",projectile)
projectilemover.CartoonFactor = 0.5
projectilemover.Target = part
projectilemover.MaxSpeed = 1000
projectilemover.TurnP = 500
projectilemover.TurnD = 10
projectilemover.ThrustP = 500
projectilemover.ThrustD = 0
projectilemover.TargetRadius = 5
projectilemover.ReachedTarget:Connect(function()
stoptrail = true
local endexplosion = Instance.new("Explosion",workspace)
endexplosion.Position = part.Position
endexplosion.BlastPressure = 100
endexplosion.DestroyJointRadiusPercent = 100
endexplosion.BlastRadius = 15
endexplosion.ExplosionType = Enum.ExplosionType.Craters
endexplosion.Visible = true
end)
repeat
wait(0.5)
explodetrail(projectile)
until stoptrail
end)
When I activate the tool, the output says: "18:20:01.210 - ServerScriptService.projectileScript:28: bad argument #2 (Vector3 expected, got nil)"
