Need help fixing my script

Hello, i need help fixing my hitbox script by making my hitbox part inside the torso increases with the torso size, i mean by making when torso size is increased +2 when collecting “Collectablesphere” model the hitbox increases too

Hitbox script

local Players = game:GetService(“Players”)

– Function to disable collision for character parts
local function disableCollision(character)
for _, part in pairs(character:GetChildren()) do
if part:IsA(“BasePart”) then
part.CanCollide = false
end
end
end

– Function to get the size of the player’s torso
local function getTorsoSize(player)
local character = player.Character
if character then
local torso = character:FindFirstChild(“Torso”) or character:FindFirstChild(“UpperTorso”)
if torso then
return torso.Size.Magnitude
end
end
return 0 – Return 0 if no torso is found
end

– Function to create the kill part
local function createKillPart(player)
local character = player.Character
if character then
local torso = character:FindFirstChild(“Torso”) or character:FindFirstChild(“UpperTorso”)
if torso then
– Disable collision for character parts
disableCollision(character)

		-- Create a new part
		local killPart = Instance.new("Part")
		killPart.Size = Vector3.new(1.12, 497.639, 1.348) -- Set the size of the part
		killPart.Transparency = 1 -- Make the part invisible (optional)
		killPart.CanCollide = false -- Disable collision (optional)
		killPart.Anchored = false -- Allow the part to move with the player
		killPart.Name = "KillPart"

		-- Parent the part to the character
		killPart.Parent = character
		-- Position the part inside the torso
		killPart.Position = torso.Position

		-- Weld the part to the torso to move with it
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = torso
		weld.Part1 = killPart
		weld.Parent = torso

		-- Connect a Touched event to the killPart
		local debounce = false
		killPart.Touched:Connect(function(hit)
			if debounce then return end -- Prevent multiple triggers at once
			debounce = true
			local otherPlayer = Players:GetPlayerFromCharacter(hit.Parent)
			if otherPlayer and otherPlayer ~= player then
				local playerTorsoSize = getTorsoSize(player)
				local otherPlayerTorsoSize = getTorsoSize(otherPlayer)
				-- Check if the player's torso size is greater than the other player's torso size
				if playerTorsoSize > otherPlayerTorsoSize then
					local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
					-- Ensure the other player's Humanoid exists and they're still alive
					if humanoid and humanoid.Health > 0 then
						humanoid.Health = 0

						-- Add +30 money to the player who kills
						local leaderstats = player:FindFirstChild("leaderstats")
						if leaderstats then
							local money = leaderstats:FindFirstChild("Money")
							if money then
								money.Value = money.Value + 30
							end
						end
					end
				end
			end
			debounce = false
		end)
	end
end

end

– Connect the PlayerAdded event to spawn the kill part and disable collision
Players.PlayerAdded:Connect(function(player)
– Ensure each player has leaderstats and a Money value
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = 0 -- Starting value for Money
money.Parent = leaderstats

player.CharacterAdded:Connect(function(character)
	createKillPart(player)
end)

end)

1 Like

Did you AI generate this code? I’m not against the practice itself, but you should know what your code is meant to do and how it’s not working.

Basically what I’m trying to say is the people of the devforum don’t want to test and proofread your code. We want to know what the issue is, and any errors in the console.

Additionally, it appears you haven’t tried to debug whatever the problem of your script is. This and the random comments explaining every other line of code gives me the impression of AI.

Please, if this is the case, the devforum is not the place to paste faulty AI code and expect new working code that is tailored to your exact game.

I’ll be willing to help you if you can tell me the issue with the code, if you have tried debugging, and what your process was while making this code.

On one last note, if you want people to bother helping you in the future, the least you can do is paste ALL of your lua code in Preformatted text. I see you only put about a third of your code in preformatted text, so it looks very messy.

Edit: This is also in the wrong category, this belongs in #help-and-feedback:scripting-support

3 Likes

I agree with @sick_tr1cks entirely.
You said Hitbox, but did you mean the killbrick?
If you just want to increase the size of the killbrick (or hitbox) then just take the Torso size and add 1 stud (or whatever you need) to each of the 3 axis sizes.

2 Likes