Hello! I want to make a script that is a child of a button, and you can toggle the button on/off. It’s goal is to hide the players names and healthbars, locally.
I have made this script, but it won’t seem to work.
Again, the script is a localscript that is a child of a TextButton. I have tested the script, and all of the prints that I put in work fine, and the button changes it’s color and text accordingly, but the names and healthbars will not hide.
local Players = game:GetService("Players")
local enabled = true
local function refresh()
local playerList = Players:GetPlayers()
for _, player in pairs(playerList) do
if player.Character then
local char = player.Character
local hum = char:WaitForChild("Humanoid")
print("Test1")
if enabled == true then
hum.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.Viewer
enabled = false
elseif enabled == false then
hum.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
enabled = true
end
end
end
local player = game:GetService("Players")
local char = player.Character
local hum = char:WaitForChild("Humanoid")
print("Test 2")
if enabled == true then
hum.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.Viewer
enabled = false
elseif enabled == false then
hum.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
enabled = true
end
end
local toggle = false
script.Parent.MouseButton1Click:Connect(function()
local playerList = Players:GetPlayers()
for _,player in pairs (playerList) do
if player.Character then
local char = player.Character
local hum = char:WaitForChild("Humanoid")
print(player)
if toggle == false then
print("Test3",hum)
toggle = true
script.Parent.Text = "ON"
script.Parent.BackgroundColor3 = Color3.fromRGB(85, 255, 127)
hum.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.Viewer
return
elseif toggle == true then
toggle = false
print("Test4",hum)
script.Parent.Text = "OFF"
script.Parent.BackgroundColor3 = Color3.fromRGB(255, 85, 88)
hum.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
return
end
end
end
end)```