How to make a script that activates something permenantly

I want to make a script that permenantly enables visibility for a part. Thing is, the part is only created from an Instance.new statement, which promptly defines it’s transparency as 0. In short, I want to make a function that changes a variable depending on if it is active or not. I’m assuming I have to put “unless” after “x.Transparency = 1”, but I don’t know what to put afterwards.

Here is my part creation script:

UIS.InputBegan:Connect(function(key,  GPE)
	if not GPE then
		if debounce == false then
			if key.UserInputType == Enum.UserInputType.MouseButton1 then
				debounce = true
				playhitanimation:Play()
				wait(0.5)
				local x = Instance.new("Part", workspace)
				x.Name = "Hitbox"
				x.BrickColor = BrickColor.new("Really red")
				x.Size = Vector3.new(4, 4, 4)
				x.CanCollide = false
				x.Transparency = 1
				local w = Instance.new("Weld", x)
				w.Part0 = Character.HumanoidRootPart
				w.Part1 = x
				w.C1 = CFrame.new(0, 0, 2)
				for i, v in pairs(workspace:GetPartsInPart(x)) do
					if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
						local x = Instance.new("IntValue", v.Parent)
						x.Name = "Hit"..Player.Name
						game.Debris:AddItem(x, 1)
						damageevent1:FireServer(v)
					end
				end
				wait(1)
				x:Destroy()
				wait(1)
				debounce = false
			end
		end
	end
end)

and my visibility script:

local rs = game:GetService("ReplicatedStorage")
local she = rs:WaitForChild("SeeingHitboxesEvent")
local hitbox = game.Workspace:FindFirstChild("Hitbox")

she.OnServerEvent:Connect(function(player)
	hitbox.Transparency = 0
end)
1 Like

Sorry if I’m repeating your question, but if I’m not misunderstanding anything here, you would like to:

  • Check in your Part Creation script if the Hitbox part was already made.
  • If it’s made, then don’t make any changes.
1 Like

Yes, that exactly. Thank you for repairing my question!

In that case, you could just make a Boolean variable to keep track in your script whether the variable is true or false in your part creation script:

local isPartActivated = false

And check if the variable is false:

if isPartActivated == false then
    isPartActivated = true
    -- Other code here.
end

Yes, but I’d also like to know how to make the function infinite. I’m assuming it’s a while true do, but idk.

Yes, it is a while true do, which gives an infinite loop.

I’m having a little issue. This is my new code:

local debounce = false
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local hitanimation = Instance.new('Animation')
hitanimation.AnimationId = 'rbxassetid://13854315509'
playhitanimation = Character.Humanoid:LoadAnimation(hitanimation)
local rs = game:GetService("ReplicatedStorage")
local damageevent1 = rs:WaitForChild("DamageEvent1")
local she = rs:WaitForChild("SeeingHitboxesEvent")
local hitboxneedstobeseen = false



UIS.InputBegan:Connect(function(key,  GPE)
	if not GPE then
		if debounce == false then
			if key.UserInputType == Enum.UserInputType.MouseButton1 then
				debounce = true
				playhitanimation:Play()
				wait(0.5)
				if hitboxneedstobeseen == false then
					local x = Instance.new("Part", workspace)
					x.Name = "Hitbox"
					x.BrickColor = BrickColor.new("Really red")
					x.Size = Vector3.new(4, 4, 4)
					x.CanCollide = false
					x.Transparency = 1
					local w = Instance.new("Weld", x)
					w.Part0 = Character.HumanoidRootPart
					w.Part1 = x
					w.C1 = CFrame.new(0, 0, 2)
					for i, v in pairs(workspace:GetPartsInPart(x)) do
						if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
							local x = Instance.new("IntValue", v.Parent)
							x.Name = "Hit"..Player.Name
							game.Debris:AddItem(x, 1)
							damageevent1:FireServer(v)
						end
					end
					wait(1)
					x:Destroy()
					wait(1)
					debounce = false
				else
					local x = Instance.new("Part", workspace)
					x.Name = "Hitbox"
					x.BrickColor = BrickColor.new("Really red")
					x.Size = Vector3.new(4, 4, 4)
					x.CanCollide = false
					x.Transparency = 0.5
					local w = Instance.new("Weld", x)
					w.Part0 = Character.HumanoidRootPart
					w.Part1 = x
					w.C1 = CFrame.new(0, 0, 2)
					for i, v in pairs(workspace:GetPartsInPart(x)) do
						if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
							local x = Instance.new("IntValue", v.Parent)
							x.Name = "Hit"..Player.Name
							game.Debris:AddItem(x, 1)
							damageevent1:FireServer(v)
						end
					end
					wait(1)
					x:Destroy()
					wait(1)
					debounce = false
				end
			end
		end
	end
end)

she.OnServerEvent:Connect(function(player)
	hitboxneedstobeseen = true
end)

And I should have put this sooner, but the code for the button:

local visibutton = script.Parent
local colorchange = false
local rs = game:GetService("ReplicatedStorage")
local she = rs:WaitForChild("SeeingHitboxesEvent")

visibutton.MouseButton1Click:Connect(function()
	if colorchange == false then
		visibutton.BackgroundColor3 = Color3.new(0, 0.392157, 0)
		colorchange = true
		she:FireServer()
	else
		visibutton.BackgroundColor3 = Color3.new(0.211765, 0.211765, 0.211765)
		colorchange = false
	end
end)

The animation is playing, the color change for the button works, but the hitbox still does not change transparency.

So other parts of the code are working as intentional? Could you perhaps send a video of this in-game?

Here’s an example of how you could create a function in Lua to permanently enable visibility for a part that has been created using Instance.new:

function permanently_enable_visibility_for_part(part)
  if part.Visible == false then
    part.Visible = true
  end
end

This function takes one argument, part, which is the part that you want to enable visibility for. It checks whether part.Visible is currently false. If it is, it sets part.Visible to true. Otherwise, it does nothing.

To use this function, you would pass it the part as an argument:

permanently_enable_visibility_for_part(Instance.new("Part", part.Parent))

This would enable the visibility of the part, assuming it was not already visible.

Note that if you want the part to be permanently visible even if it is regenerated or replaced, you may need to attach this function to a future function that runs every frame. This way, it will be called every time the part is loaded, regardless of how or when it was created.

For some reason I can’t upload the file. But it’s worse than I thought.

she.OnServerEvent:Connect(function(player)
	hitboxneedstobeseen = true
end)

This function is at the top of my script to dictate that when the button is pressed, the remote event is fired and this is activated. Instead, this breaks the entire script, whether I activate it or not. This is the entire script.

local debounce = false
local sprintdebounce = false
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local hitanimation = Instance.new('Animation')
hitanimation.AnimationId = 'rbxassetid://13854315509'
playhitanimation = Character.Humanoid:LoadAnimation(hitanimation)
local rs = game:GetService("ReplicatedStorage")
local damageevent1 = rs:WaitForChild("DamageEvent1")
local she = rs:WaitForChild("SeeingHitboxesEvent")
local hitboxneedstobeseen = false
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://14191010636'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)

she.OnServerEvent:Connect(function(player)
	hitboxneedstobeseen = true
end)



UIS.InputBegan:Connect(function(key,  GPE)
	if not GPE then
		if debounce == false then
			if key.UserInputType == Enum.UserInputType.MouseButton1 then
				sprintdebounce = true
				debounce = true
				PlayAnim:Stop()
				playhitanimation:Play()
				Character.Humanoid.WalkSpeed = 2
				wait(0.5)
				if hitboxneedstobeseen == false then
					local x = Instance.new("Part", workspace)
					x.Name = "Hitbox"
					x.BrickColor = BrickColor.new("Really red")
					x.Size = Vector3.new(4, 4, 7)
					x.CanCollide = false
					x.Transparency = 1
					local w = Instance.new("Weld", x)
					w.Part0 = Character.HumanoidRootPart
					w.Part1 = x
					w.C1 = CFrame.new(0, 0, 2)
					for i, v in pairs(workspace:GetPartsInPart(x)) do
						if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
							local x = Instance.new("IntValue", v.Parent)
							x.Name = "Hit"..Player.Name
							game.Debris:AddItem(x, 1)
							local mark = Instance.new("Highlight", v.Parent)
							mark.FillTransparency = 1
							mark.OutlineColor = Color3.new(1, 1, 0)
							damageevent1:FireServer(v)
						end
					end
					Character.Humanoid.WalkSpeed = 10
					sprintdebounce = false
					wait(1)
					x:Destroy()
					wait(1)
					debounce = false
				else
					local x = Instance.new("Part", workspace)
					x.Name = "Hitbox"
					x.BrickColor = BrickColor.new("Really red")
					x.Size = Vector3.new(4, 4, 7)
					x.CanCollide = false
					x.Transparency = 0.5
					local w = Instance.new("Weld", x)
					w.Part0 = Character.HumanoidRootPart
					w.Part1 = x
					w.C1 = CFrame.new(0, 0, 2)
					for i, v in pairs(workspace:GetPartsInPart(x)) do
						if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
							local x = Instance.new("IntValue", v.Parent)
							x.Name = "Hit"..Player.Name
							game.Debris:AddItem(x, 1)
							damageevent1:FireServer(v)
						end
					end
					wait(1)
					x:Destroy()
					wait(1)
					debounce = false
				end
			end
		end
	end
end)

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and sprintdebounce == false then
		Character.Humanoid.WalkSpeed = 20
		PlayAnim:Play()
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 10
		PlayAnim:Stop()
	end
end)

I genuinely don’t know why, but it sucks.