Change Objects (make visible) in a local script

If the player presses Shift + R, the hitboxes of every player should appear. (only for this one player)

I’m having trouble figuring out how to make it change for just **one person. **

I have tried to look around on the internet for solutions but have not found anything suitable. I then have tried creating them by myself. Firstly, I created the hitbox on server, then tried accessing it with a local script. It didn’t work. Secondly, I tried creating them on local script, it didn’t work as well. Thirdly, (suggestion from chatgpt), i created them on server and sent the instance to the local script. This one didnt work as well.

My current script:

Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
-- Hitbox:
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		if humanoidRootPart then
			-- HitboxPart:
			local hitbox = Instance.new("Part", humanoidRootPart)
			hitbox.Name = "HitboxPart"
			hitbox.Size = Vector3.new(4.184, 5.328, 3.05)
			hitbox.Position = Vector3.new(0.115, 3.611, -0.439)

			--HitboxOutline:
			local hitboxOutline = Instance.new("SelectionBox", hitbox)
			hitboxOutline.Color3 = Color3.new(0.67451, 0.345098, 0.352941)
			hitboxOutline.LineThickness = 0.03
			hitboxOutline.SurfaceTransparency = 1
			hitboxOutline.Visible = false
			
			local allOutlines = {}
			for i, v in pairs(Players:GetPlayers()) do
				local character  = v.Character or v.CharacterAdded:Wait()
				if character then
					local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
					if humanoidRootPart then
						local hitboxPart = humanoidRootPart:FindFirstChild("HitboxPart")
						if hitboxPart then
							local SelectionBox = hitboxPart:FindFirstChild("SelectionBox")
							if SelectionBox then
								if i == 1 then
									HitboxRE:FireAllClients(SelectionBox, "reset")
								else
									HitboxRE:FireAllClients(SelectionBox)
								end
							end
						end
					end
				end
			end
end)

Any help is appreciated!

1 Like

This should only take place in a local script, because nothing needs to be verified on the server. Please give the local script you wrote that doesn’t work.

2 Likes

Thanks for the reply!

It took a bit, but I figured it out by myself.

Here’s the local script im using to create the hitbox for each player and NPC:

-- Show Hitbox:
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R and UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) then -- Shift + R to enable ViewHitboxMode
		if not hitbox_Visible then -- turns it on
			hitbox_Visible = true
			-- Player:
			for i, v in pairs(Players:GetPlayers()) do
				local character = player.Character or player.CharacterAdded:Wait()
				local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
				if humanoidRootPart then
					-- HitboxPart:
					local hitbox = Instance.new("Part", humanoidRootPart)
					hitbox.Name = "HitboxPart_Local"
					hitbox.Size = Vector3.new(4.184, 5.528, 3.05)
					hitbox.Position = humanoidRootPart.Position
					hitbox.CFrame = humanoidRootPart.CFrame
					hitbox.CanCollide = false
					hitbox.Transparency = 0.9

					-- Weld hitbox:
					local Weld = Instance.new("WeldConstraint", hitbox)
					Weld.Name = "WeldConstraint_Local"
					Weld.Part1 = hitbox
					Weld.Part0 = humanoidRootPart

					--HitboxOutline:
					local hitboxOutline = Instance.new("SelectionBox", hitbox)
					hitboxOutline.Name = "SelectionBox_Local"
					hitboxOutline.Color3 = Color3.new(0.67451, 0.345098, 0.352941)
					hitboxOutline.LineThickness = 0.03
					hitboxOutline.SurfaceTransparency = 1
					hitboxOutline.Transparency = 0
					hitboxOutline.Visible = true
					hitboxOutline.Adornee = hitbox
				end
			end
			
			for i, v in pairs(workspace.NPC_Folder:GetChildren()) do
				local humanoidRootPart = v:FindFirstChild("HumanoidRootPart")
				if humanoidRootPart then
					-- HitboxPart:
					local hitbox = Instance.new("Part", humanoidRootPart)
					hitbox.Name = "HitboxPart_Local"
					hitbox.Size = Vector3.new(4.184, 5.528, 3.05)
					hitbox.Position = humanoidRootPart.Position
					hitbox.CFrame = humanoidRootPart.CFrame
					hitbox.CanCollide = false
					hitbox.Transparency = 0.9

					-- Weld hitbox:
					local Weld = Instance.new("WeldConstraint", hitbox)
					Weld.Name = "WeldConstraint_Local"
					Weld.Part1 = hitbox
					Weld.Part0 = humanoidRootPart

					--HitboxOutline:
					local hitboxOutline = Instance.new("SelectionBox", hitbox)
					hitboxOutline.Name = "SelectionBox_Local"
					hitboxOutline.Color3 = Color3.new(0.67451, 0.345098, 0.352941)
					hitboxOutline.LineThickness = 0.03
					hitboxOutline.SurfaceTransparency = 1
					hitboxOutline.Transparency = 0
					hitboxOutline.Visible = true
					hitboxOutline.Adornee = hitbox
				end
			end
		elseif hitbox_Visible then -- turns it off
			-- Player:
			for i, v in pairs(Players:GetPlayers()) do
				local character = player.Character or player.CharacterAdded:Wait()
				local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
				if humanoidRootPart then
					for i, v in pairs(humanoidRootPart:GetDescendants()) do

						if v.Name == "HitboxPart_Local" then
							Debris:AddItem(v, 0)
						end
						if v.Name == "SelectionBox_Local" then
							Debris:AddItem(v, 0)
						end
						if v.Name == "WeldConstraint_Local" then
							Debris:AddItem(v, 0)
						end
					end
				end
			end
			
			-- NPC:
			for i,v in pairs(workspace.NPC_Folder:GetChildren()) do
				local humanoidRootPart = v:FindFirstChild("HumanoidRootPart")
				if humanoidRootPart then
					for i, v in pairs(humanoidRootPart:GetDescendants()) do
						if v.Name == "HitboxPart_Local" then
							Debris:AddItem(v, 0)
						end
						if v.Name == "SelectionBox_Local" then
							Debris:AddItem(v, 0)
						end
						if v.Name == "WeldConstraint_Local" then
							Debris:AddItem(v, 0)
						end
					end
				end
			end
			hitbox_Visible = false
		end
	end
end)

I didnt want to create a new one every single time and delete it after that but I guess I have no choice.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.