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

Variable not updating as it should?

Asked by 1 year ago
Edited by Xapelize 1 year ago

(code is messy, sorry its one of my first scripts)

What this code is supposed to do is recieve a variable from a script inside a click detector that will activate a selection mode to the player who clicked it, and only for the player who clicked it untill the player closes or leaves (i did not add a way to close it yet) and thats because my selection code is not working because what the code is supposed to do is:

----------------------------------------------------------AIMING AT SCREEN
    for i,v in pairs(SELECTED) do -- error here (SCREEN marks the LAST screen, needs to be updated more?)
        if v == SCREEN then
            INDEX = i
            AAS = true
        elseif v ~= SCREEN then
            AAS = false -- AAS = Aiming At Screen then
        end 
    end

and it does kinda work but when i click to select it, i have to unselect it in the reverse order i clicked them or else it breaks, and im pretty sure its because of the code above, i will send the full code if anyone is interested ( any code with -- before it, its unused code i just did not want to delete)

local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")

local CLICKED = nil

local plr = game.Players.LocalPlayer

local mouse = plr:GetMouse()

local HL = game.ReplicatedFirst:WaitForChild("SELECTOR HL")

SELECTED = {}

REMOVED = {}

--ADDED = false

NAMEHL = Instance.new("Highlight")

T = false

local CHECK = Instance.new("BoolValue")

HIGL = CHECK

mouse.Button1Down:Connect(function()

    if CLICKED == true then

        --print("test1")

        if CANCLICK == true then

            --HTS = hit.Parent:FindFirstChild("SCREEN")

            --HTSC = HTS:FindFirstChild("Value")

            --if HTSC == nil then

            --  AAS = false

            --else

            --  AAS = true

            --end

            if AAS == true then

                for i,v in pairs(SELECTED) do

                    if i == INDEX then            -- REMOVE

                        REMOVE = v

                        table.insert(REMOVED,REMOVE)

                        table.remove(SELECTED,i)

                        print("REMOVE")

                        print(SELECTED,REMOVED)

                    end

                    test = table.getn(SELECTED)

                    if test == 0 then

                        AAS = false

                    end
                end
            else
                print("ADD")                       -- ADD
                table.insert(SELECTED, SCREEN)
                print(SELECTED,REMOVED)
        end
---------------------------------------------
            --if SELECTED == {nil} then
            --  table.insert(SELECTED,SCREEN)
            --  print(SELECTED)
            --  print("test5")
                --ADDED = true
            --end
        else
            -- not aiming at LED
        end
    end
end)
remote.OnClientEvent:Connect(function(data)
    CLICKED = data
    print(CLICKED)
end)

while true do

    wait(0.01)

    print(AAS)

    --print(HL.Parent)

    --print(CLICKED)

    hit = mouse.Target

    --print(hit)

    -------------------------------------------------------------ADD HL
    for i,v in pairs(SELECTED) do

        if v ~= false then

            if v:FindFirstChild("HL") == nil then
                local HL1 = Instance.new("Highlight")
                HL1.Parent = v
                HL1.Name = "HL"
                HL1.FillTransparency = 0.5
                HL1.OutlineTransparency = 0
                HL1.DepthMode = Enum.HighlightDepthMode.AlwaysOnTop
                HL1.Enabled = true
                HL1.FillColor = Color3.new(1, 1, 1)
                HL1.OutlineColor = Color3.new(0, 0, 0)
                NAMEHL = HL1
                TESTING = Instance.new("BoolValue")
                TESTING.Parent = v

            end
        end
    end
    --------------------------------------------------------------REMOVE
    for i,v in pairs(REMOVED) do
        print(REMOVED,"REMOVED LIST")
        HL1 = v:FindFirstChild("HL")
        NAME1 = HL1
        NAME2 = HL.Name
        if NAME1 == NAME2 then
        else
            print(HL1)
            TESTING:Destroy()
            if HL1 == nil then
            else
                HL1:Destroy()
            end
            --if v:FindFirstChild("Value") ~= nil then
            --  local A = v:FindFirstChild("Value")
            --  A:Destroy()
            --end
            table.remove(REMOVED,i)

        end
    end
    ----------------------------------------------------------AIMING AT SCREEN
    for i,v in pairs(SELECTED) do -- error here (SCREEN marks the LAST screen, needs to be updated more?)
        if v == SCREEN then
            INDEX = i
            AAS = true
        elseif v ~= SCREEN then
            AAS = false -- AAS = Aiming At Screen then
        end 
    end
    --------------------------------------------------------AIM AT SCREEN
    if hit == nil then
    else
        if hit.Parent ~= nil then
            --print(SCREEN)
            PHIT = hit.Parent
            if CLICKED == true then
                if hit.Parent:FindFirstChild("IsGate") ~= nil then

                    GATE = hit.Parent:FindFirstChild("IsGate")
                    VALUE = GATE.Value
                    if VALUE == 12 then
                        LED = GATE.Parent:WaitForChild("SCREEN")
                        MODEL = LED.Parent
                        SCREEN = PHIT:FindFirstChild("SCREEN") -- SCREEN = Last LED hit
                        CANCLICK = true
                        CHILDRENHL = SCREEN:FindFirstChild("Value")
                        --print(CHILDRENHL)

                        if CHILDRENHL == nil then
                            HL.Parent = LED
                        else
                            HL.Parent = game.ReplicatedFirst

                    end
                end
            end
        end 
    end
    if MODEL ~= PHIT then
        CHILDRENHL = false
        CANCLICK = false
            HL.Parent = game.ReplicatedFirst
            end
        end
    end

--if v == SCREEN then

--  if TEST ~= true then

--      print("REMOVE")

--      table.insert(REMOVED, v)

--      table.remove(SELECTED, i)

--      --ADDED = false

--  else

--      TEST = false

--  end

--else

--  print(v)
--  table.insert(SELECTED,SCREEN)
--  print(SELECTED)
--  print("ADD")
--  TEST = true
--end

1 answer

Log in to vote
0
Answered by 1 year ago

update: i used another code of mine and it worked, by changing some stuff around etc if anyone wants to see the code here it is:

                for i,v in pairs(SELECTED) do         -- !
                    local CurrentV = v
                    if mouse.Target.Parent:FindFirstChild("SCREEN") == v then
                        Skip = true
                        --print(Skip)
                        table.insert(REMOVED,v)
                        table.remove(SELECTED,i)
                        print("removed from table")

                    end
                end                                         -- .

                if Skip == true then
                    Skip = false
                    print("skipped")
                elseif Skip == false then
                    --print(Skip)
                    print("added to table")
                    table.insert(SELECTED,mouse.Target.Parent:FindFirstChild("SCREEN"))
                    --print(SelectedINPUTS)
                else
                    print("ERROR SKIP ~= TRUE/FALSE")
                end

now its just some other errors that i need to fix and i think its done

0
update: it now works, im just going to finish it up, except im not even close to finishing it lol JP_Pontes 5 — 1y
Ad

Answer this question