Hello again, developers. For my newest project, I need a hitbox to be constantly attached to the player so when they click, they punch and anything caught in that hitbox takes damage. I have this hitbox stored in Replicated Storage, and am attempting to use a sever sided script with a function that runs on player join to clone this part, move it in front of the player and weld it to the humanoid root part.
I have a check to test if the part was properly cloned, and every time, it runs like it was cloned, yet checking explorer shows it’s nowhere to be found. It’s like its existing in some sort of limbo.
Any help would be appreciated. And yes, I know this isn’t the best way to do hitboxes.
Ignore the many code comments, a lot of them are for my team create companions who can’t code.
local PlayerService = game:GetService("Players")
function addPunchHitbox(plr) --Do I really have to explain this??
plr.CharacterAdded:Connect(function(char)
if char:FindFirstChildOfClass("Humanoid") ~= nil then
task.wait(2)
local keepHitboxConstant = Instance.new("Weld")
local root = char:FindFirstChild("HumanoidRootPart")
local lookDirection = root.CFrame.LookVector
local hit = game:GetService("ReplicatedStorage"):FindFirstChild("M1Box"):Clone() --I don't know why, I don't want to know why, I shouldn't have
--to wonder why, but this is technically more efficient than .ReplicatedStorage.M1Box:Clone(). It's so dang long. Kill me now.
if hit ~= nil then --check if clone was succesful
local offset = 0.25 -- studs in front of the player
hit.CFrame = root.CFrame + lookDirection * offset --place the part [offset] amount of studs in front of the player
hit.Parent = char --class part as "child" of player, for ease of access and testing purposes
print('moved i think')
keepHitboxConstant.Part0 = root -- attach hitbox to the player
keepHitboxConstant.Part1 = hit --make sure hitbox moves with the player, not the other way around
print ('attached')
end
else --log if not
print('something went wrong, couldnt clone')
end
end)
end
PlayerService.PlayerAdded:Connect(function(plr) --run both functions when a player joins
addPunchHitbox(plr)
-- addBaseMoveset(plr) ignore, unused for now
end)```
