This script has to do some stuff with a gui I made if the values change. But it doesn't do anything
local plyr = script.Parent.Parent.Parent local tool = plyr.Tool local ammo = plyr.Tool.Ammo local gui = script.Parent local armor = plyr.Armor local clip = plyr.Tool.Clip while true do if tool.Value == "Hands" then gui.Index.Screen.Title.Text = "Hands - No Tool" gui.Index.Screen.Ammo.Visible = false gui.Index.Screen.Ammo2.Visible = false gui.Index.Screen.Controls.Visible = false gui.Index.Screen.LEFT.Visible = false gui.Index.Screen.RIGHT.Visible = false gui.Index.Screen.MODE.Visible = false gui.BG:TweenPosition(UDim2.new(1, -325, 1, -75)) gui.Index:TweenPosition(UDim2.new(1, -310, 1, -60)) elseif tool.Value == "Test" then gui.Index.Screen.Title.Text = "Test - ammothing" gui.Index.Screen.Ammo.Visible = true gui.Index.Screen.Ammo2.Visible = false gui.Index.Screen.Controls.Visible = true gui.Index.Screen.LEFT.Visible = false gui.Index.Screen.RIGHT.Visible = false gui.Index.Screen.MODE.Visible = true gui.Index.Screen.Ammo.Text = ammo.Value.."/"..clip.Value gui.BG:TweenPosition(UDim2.new(1, -325, 1, -125)) gui.Index:TweenPosition(UDim2.new(1, -310, 1, -110)) end wait(1) end while true do if armor.Value == "0" then gui.Index2.Armor.Visible = false gui.Index2.ARMOR.Visible = false gui.BG:TweenPosition(UDim2.new(0, 0, 1, -75)) gui.Index:TweenPosition(UDim2.new(0, 20, 1, -60)) end wait(1) end
I tried it with the .changed but it didn't work
local plyr = script.Parent.Parent.Parent local tool = plyr.Tool local ammo = plyr.Tool.Ammo local gui = script.Parent local armor = plyr.Armor local clip = plyr.Tool.Clip while true do if armor.Value == "0" then gui.Index2.Armor.Visible = false gui.Index2.ARMOR.Visible = false gui.BG:TweenPosition(UDim2.new(0, 0, 1, -75)) gui.Index:TweenPosition(UDim2.new(0, 20, 1, -60)) end wait(1) end armor.Changed:connect(function(NewValue) if NewValue == "0" then gui.Index2.Armor.Visible = false gui.Index2.ARMOR.Visible = false gui.BG:TweenPosition(UDim2.new(0, 0, 1, -75)) gui.Index:TweenPosition(UDim2.new(0, 20, 1, -60)) end end) tool.Changed:connect(function(NewValue) if NewValue == "Test" then gui.Index.Screen.Title.Text = "Test - ammothing" gui.Index.Screen.Ammo.Visible = true gui.Index.Screen.Ammo2.Visible = false gui.Index.Screen.Controls.Visible = true gui.Index.Screen.LEFT.Visible = false gui.Index.Screen.RIGHT.Visible = false gui.Index.Screen.MODE.Visible = true gui.Index.Screen.Ammo.Text = ammo.Value.."/"..clip.Value gui.BG:TweenPosition(UDim2.new(1, -325, 1, -125)) gui.Index:TweenPosition(UDim2.new(1, -310, 1, -110)) elseif NewValue == "Hands" then gui.Index.Screen.Title.Text = "Hands - No Tool" gui.Index.Screen.Ammo.Visible = false gui.Index.Screen.Ammo2.Visible = false gui.Index.Screen.Controls.Visible = false gui.Index.Screen.LEFT.Visible = false gui.Index.Screen.RIGHT.Visible = false gui.Index.Screen.MODE.Visible = false gui.BG:TweenPosition(UDim2.new(1, -325, 1, -75)) gui.Index:TweenPosition(UDim2.new(1, -310, 1, -60)) end end)
As stated by GoldenPhysics, using .Changed
is a better solution in this case because it will only run when the values are changed rather than having the compiler check the conditional constantly. Another disadvantage of using a while true loop is the fact that the compiler will never get past that loop to code further down the line without a 'break'.
Always use the LocalPlayer
property of Players to get the player from a local script. Using a series of 'script.Parent's isn't effecient.
To change your code into using .Changed is rather simple:
local plyr = game.Players.LocalPlayer local tool = plyr.Tool local ammo = plyr.Tool.Ammo local gui = script.Parent local armor = plyr.Armor local clip = plyr.Tool.Clip local screen = gui.Index["Screen"] tool.Changed:connect(function() if tool.Value == "Hands" then screen.Title.Text = "Hands - No Tool" screen.Ammo.Visible = false screen.Ammo2.Visible = false screen.Controls.Visible = false screen.LEFT.Visible = false screen.RIGHT.Visible = false screen.MODE.Visible = false gui.BG:TweenPosition(UDim2.new(1, -325, 1, -75)) gui.Index:TweenPosition(UDim2.new(1, -310, 1, -60)) elseif tool.Value == "Test" then screen.Title.Text = "Test - ammothing" screen.Ammo.Visible = true screen.Ammo2.Visible = false screen.Controls.Visible = true screen.LEFT.Visible = false screen.RIGHT.Visible = false screen.MODE.Visible = true screen.Ammo.Text = ammo.Value.."/"..clip.Value gui.BG:TweenPosition(UDim2.new(1, -325, 1, -125)) gui.Index:TweenPosition(UDim2.new(1, -310, 1, -110)) end end) armor.Changed:connect(function() if armor.Value == "0" then gui.Index2.Armor.Visible = false gui.Index2.ARMOR.Visible = false gui.BG:TweenPosition(UDim2.new(0, 0, 1, -75)) gui.Index:TweenPosition(UDim2.new(0, 20, 1, -60)) end end)