Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone tell me what is wrong with this script?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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)
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? DigitalVeer 1473 — 9y
0
its a localscript in a gui, idk why I have the second one, but now I know why that one doesn't work. GreyLuca 30 — 9y
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. GoldenPhysics 474 — 9y
0
ok, I will try it with .changed. GreyLuca 30 — 9y
0
it didn't work, I will edit the question and post the new script GreyLuca 30 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

.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)

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? GreyLuca 30 — 9y
Ad

Answer this question