Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can someone tell me what is wrong with this script?

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)

5 comments

0

What type of script is this? What error, if any, are you getting? Why do you have two 'while true do' loops when the second one will never be reached?

0

its a localscript in a gui, idk why I have the second one, but now I know why that one doesn't work.

0

I suggest you look up the .changed event on the wiki. It'll keep your game from having to check the value every second.

0

ok, I will try it with .changed.

0

it didn't work, I will edit the question and post the new script

1 answer

#.Changed

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)

1 comment

0

because it still doesn't work I start to think that something is wrong with the tool that changes the value. should I post this to?

This discussion is preserved as it was. New answers, comments, and votes are closed.