Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I apply a hitbox to all players?

Hi. I'd like to apply a part to all players when they join the game, but I lack the experience. Could someone please give me an example of how this can be done? Thanks!

2 answers

✓ Accepted answer
-- welds a part to everyone that spawns

local Players = game:GetService("Players")

function weldBetween(a, b)
	local weld = Instance.new("ManualWeld", a)
	weld.Name = "Weld"
	weld.C0 = a.CFrame:inverse() * b.CFrame
	weld.Part0 = a
	weld.Part1 = b
	return weld
end

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Root = Character:WaitForChild("HumanoidRootPart")

		local part = Instance.new("Part")
		-- customize the part however you want here
		part.CanCollide = false
		part.Massless = true
		part.Size = Vector3.new(4, 5, 2)
		part.Transparency = .5
		
		part.CFrame = Root.CFrame
		part.Parent = Character		
		weldBetween(part, Root)
	end)
end)

1 comment

0

Thanks

To detect when a player would join the game, you could use this:

game.Players.PlayerAdded:Connect(function(player)
	
end)

and if you want to add a hitbox, you might put it in ServerStorage and make a clone of it to put in the player, perhaps like this

game.Players.PlayerAdded:Connect(function(player)
	local clone = game.ServerStorage.hitbox:Clone()
	clone.Parent = player.Character
end)

But from then you would need to have a script inside the hit box to make sure it stays on the player at all times, maybe like this

local hitbox = script.Parent
local character = script.Parent.Parent
while wait(0.05) do
	hitbox.Position = character.HumanoidRootPart.Position
end

But in the end, I would think it would just be easier to detect a hit using the HumanoidRootPart inisde a player

This discussion is preserved as it was. New answers, comments, and votes are closed.