Hey so I’m making this throwing spear attack for my games boss fight and for some reason the StatusEffectsPlayer function is only fired sometimes upon the spear landing into the player. The glitch never happens if the spear hits anything other than the torso or the humanoidrootpart. I’ve included a video along with the code below.
-- Get Services
local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Get Objects
local Spear = ServerStorage:WaitForChild("Spear")
local Boss = script.Parent:WaitForChild("BossSpearThrow")
-- Get Folders
local SpearFolder = script.Parent:WaitForChild("SpearFolder")
local function StatusEffectsPlayer(player, character)
print(player.Name, "is the players name")
print(character.Name, "is the characters name")
end
-- Function to fire spear
local function FireSpear()
-- Clone spear and set it up
local SpearClone = Spear:Clone()
SpearClone.Parent = SpearFolder
-- Make sure SpearClone has a PrimaryPart set
if not SpearClone.PrimaryPart then
warn("Spear clone does not have a PrimaryPart!")
return
end
-- Position the spear relative to the boss
local bossCFrame = Boss.HumanoidRootPart.CFrame
SpearClone:SetPrimaryPartCFrame(bossCFrame * CFrame.new(0, 10, 0)) -- Offset it 10 studs in front
-- Find a random player
local players = game:GetService("Players"):GetPlayers()
local randomPlayer = players[math.random(1, #players)] -- Pick a random player
if not randomPlayer or not randomPlayer.Character then
SpearClone:Destroy()
return
end
-- Get the random player's HumanoidRootPart (target)
local targetPosition = randomPlayer.Character:WaitForChild("HumanoidRootPart").Position
-- Get the MainPart of the spear
local FacePart = SpearClone:WaitForChild("MainPart")
-- Get the Sound
local ArrowWoosh = FacePart:WaitForChild("ArrowWoosh")
ArrowWoosh:Play()
-- Calculate the direction to the target
local direction = (targetPosition - FacePart.Position).unit -- Get the direction vector
FacePart.CFrame = CFrame.lookAt(FacePart.Position, targetPosition) -- Face the target once
-- Variables for smooth movement
local speed = 120 -- Move 120 studs per second
local distanceTraveled = 0
local maxDistance = 1000
local isHit = false -- Flag to prevent multiple hit clones
-- Check for collisions
local function checkForCollision()
local ray = RaycastParams.new()
ray.FilterDescendantsInstances = {SpearClone} -- Ignore the spear itself
local rayResult = workspace:Raycast(FacePart.Position, FacePart.CFrame.LookVector * 1, ray) -- Short ray to check collisions
return rayResult
end
-- Use RenderStepped for smooth movement
RunService.Heartbeat:Connect(function(deltaTime)
-- Move the spear forward each frame
if distanceTraveled < maxDistance then
local moveDistance = speed * deltaTime
distanceTraveled = distanceTraveled + moveDistance
-- Update spear position
FacePart.CFrame = FacePart.CFrame + FacePart.CFrame.LookVector * moveDistance
-- Check for collision
local collision = checkForCollision()
if collision and not isHit then
-- If hit, stop and wait for 1 second
isHit = true -- Set the hit flag to true
-- Check if the hit part is part of a player's character
local hitPart = collision.Instance
-- Clone DeadSpear and place it at the hit location
local DeadSpearClone = ServerStorage:WaitForChild("DeadSpear"):Clone()
DeadSpearClone.Parent = SpearFolder
-- Ensure the DeadSpearClone is in the correct position and orientation
local lastCFrame = FacePart.CFrame -- Keep the same position and orientation
DeadSpearClone:SetPrimaryPartCFrame(lastCFrame)
local NewWeld = Instance.new("WeldConstraint")
NewWeld.Parent = DeadSpearClone.MainPart
NewWeld.Part0 = DeadSpearClone.MainPart
NewWeld.Part1 = hitPart
DeadSpearClone.MainPart.Anchored = false
-- If hit a player then apply debuffs to player
if hitPart.Parent:FindFirstChild("Humanoid") then
local character = hitPart.Parent
local player = game.Players:GetPlayerFromCharacter(character) -- Get player
StatusEffectsPlayer(player, character)
end
if hitPart.Name == "HumanoidRootPart" then
print("Hit the players HumanoidRottPart")
end
if hitPart.Name == "Torso" then
print("Hit torso")
end
SpearClone:Destroy() -- Destroy the original spear
return
end
else
-- Destroy spear after reaching max distance
SpearClone:Destroy()
end
end)
end
local Boss = script.Parent:WaitForChild("BossSpearThrow")
local humanoid = Boss:FindFirstChild("Humanoid")
local LanceThrow = Boss:FindFirstChild("LanceThrow")
if not humanoid then
warn("Humanoid not found!")
return
end
if not LanceThrow then
warn("Stomp animation not found!")
return
end
-- Load the Lance animation
local LanceTrack = humanoid:LoadAnimation(LanceThrow)
-- Loop the attack
while true do
wait(1.8)
LanceTrack:Play()
wait(0.2)
for i = 1, 3 do
-- Call the function for testing
FireSpear()
wait(1)
end
wait(3)
for _, child in ipairs(SpearFolder:GetChildren()) do
if child.Name == "DeadSpear" then
child:Destroy()
end
end
end