i have this script. the problem is that the script is unable to delete all welds from all hitboxes connected to it. this means that the players will end up connecting to the ball together. i believe it has to do with the weld being a child of the hitbox however i cannot change the parent because other scripts are dependant on that. how can i fix this?
local ball = script.Parent
ball.Touched:Connect(function(hit)
local hitbox = hit.Parent:FindFirstChild('HitboxPart')
if hitbox then
if hitbox:FindFirstChild("Weld") then
ball.CanCollide = true
hitbox:FindFirstChild("Weld"):Destroy()
hit.Parent.hasBall.Value = false
elseif not hitbox:FindFirstChild("Weld") then
if hit.Parent.Humanoid and hit.Parent.HitboxPart then
ball.CanCollide = true
hit.Parent.hasBall.Value = true
local player = hit.Parent
local hrp = player:FindFirstChild('HumanoidRootPart')
local weld = Instance.new("Weld", hitbox)
weld.Part0 = ball
weld.Part1 = hrp
local direction = Vector3.new(0.5,-2.4,-1.5)
local pos = hrp.CFrame:PointToWorldSpace(direction)
ball.Position = pos
end
end
elseif hit.Parent.Name ~= "Hitbox" then
end
end)
the weld is inside the hitbox, yeah, but the ball is welded to the humanoidrootpart. i really dont care which part the ball is welded to so if it has anything to do with that than i can change it but im not sure.
Not sure I understand your ultimate goal but here is something that allows transfer of a ball from player to player with a weld:
local ball = script.Parent
ball.Touched:Connect(function(hit)
if hit.Parent.Name ~= "Workspace" then
ball.CanTouch = false -- deny new request
-- check if it is a character
if hit.Parent:FindFirstChild("HumanoidRootPart") then
if not hit.Parent.HumanoidRootPart:FindFirstChild("BallWeld") then -- not
-- remove curent owner
local currentOwner = ball.Object.Value
if currentOwner ~= nil then
if currentOwner:FindFirstChild("HumanoidRootPart") then
if currentOwner.HumanoidRootPart:FindFirstChild("BallWeld") then
currentOwner.HumanoidRootPart.BallWeld:Destroy()
end
end
end
task.wait()
currentOwner = nil
-- make this character new owner
ball.Object.Value = hit.Parent
local weld = Instance.new("Weld", hit.Parent.HumanoidRootPart)
weld.Name = "BallWeld"
weld.Part0 = ball
ball.CFrame = hit.Parent.HumanoidRootPart.CFrame
weld.Part1 = hit.Parent.HumanoidRootPart
task.wait()
weld = nil
end
end
task.wait(1) -- give time for player to move away
ball.CanTouch = true -- allow new request
end
end)
Your script didn’t necessarily help me with anything, however you indirectly taught me about object values. instead of having the current player for the ball, i fixed the other scripts to use the current ball for the players. that way, i was able to fix the weld so it’s parent could still be the ball.