#Duplicating for loops!

127 messages · Page 1 of 1 (latest)

hushed basin
#

Bug, pls help!

#
local repStor = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")
local remote = repStor:WaitForChild("BuySkillFunc")
local getTowersFunc = repStor:WaitForChild("GetTowersFunc")
local requestTowerInfoFunc = repStor:WaitForChild("RequestTowerInfoFunc")
local twrStatsFunc = repStor:WaitForChild("TowerStatsFunc")
local bought = true

local plrSerivce = game:GetService("Players")
local plr = plrSerivce.LocalPlayer
--PathFrames
local SkillTreeFrame = script.Parent.SkillTreeFrame

function buyTower(towerName)
    local suc = remote:InvokeServer(towerName)
    
    if suc then
        return true
    else
        return false
    end
end

function confirm(Button, lvl)
    local statsFrame = SkillTreeFrame:WaitForChild("Stats")
    local tower = repStor.Towers[Button.Name][lvl]
    if tower then
        return true
    else
        return false
    end
end

First part
#
function purchase(Button)
    SkillTreeFrame.Stats.Purchase.Activated:Connect(function()
        local sucsess = buyTower(Button.Name)
        print("Checking sucsess")
        if sucsess then
            Button.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
            return true
        else
            return false
        end
    end)
end

script.Parent.SkillTreeButton.Activated:Connect(function()
    if SkillTreeFrame.Visible == false then
        SkillTreeFrame.Visible = true
        local towers = getTowersFunc:InvokeServer(plr)
        for tower, i in pairs(towers) do
            if i.HasTroop == true then
                local exists = script.Parent.SkillTreeFrame:FindFirstChild(tower)
                if exists then
                    exists.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
                end
            end
        end
    elseif SkillTreeFrame.Visible == true then
        SkillTreeFrame.Visible = false
        SkillTreeFrame.Stats.Visible = false
    end
    
    for i, Button in ipairs(script.Parent.SkillTreeFrame:GetChildren()) do
        if Button:IsA("TextButton") then
            if Button.Name ~= "SkillTreeButton" then
                Button.Activated:Connect(function()
                    local confirmed = confirm(Button, 0)
                    if confirmed then
                        bought = purchase(Button)
                    end
                end)
            end    
        end
        if not bought then
            print("Breaking")
            break
        end
    end
end)
#

Second

#

its running the function evrey time and not breaking

#

idk why

slender sapphire
#

Where excatly? Im not gonna read the whole code

hushed basin
#

so in the

#

purchase function

#

it runs multiple times

#

so I click a gui button, it runs the function when I click a different button, but if I click another button (to change the visuals) then click the same button (that runs the puchase func) it then runs twice

#

and keeps adding evreytime I click one to change visuals

fringe salmonBOT
#

studio** You are now Level 6! **studio

slender sapphire
#

Would you mind making video? I kinda not getting it.
Did you wanted to disable button once bought? 🤔

hushed basin
#

yeah, ill show u

#

I gotta comrpess the video rq

#

So around 18 seconds its good

#

but then when I click those green buttons (to change what stats Im seeing) and then click purchase, it duplicates the for loop and runs the function multiple times

#

witch is rly bad because its a datastore request and I dont want it to crash my game :/

slender sapphire
#

Okay, i see now the reason, when you click the button (this part)

 Button.Activated:Connect(function()
                    local confirmed = confirm(Button, 0)
                    if confirmed then
                        bought = purchase(Button)
                    end
                end)

It will fire the function everytime, which means, it will create x amounts of trigger in

function purchase(Button)
    SkillTreeFrame.Stats.Purchase.Activated:Connect(function()
        local sucsess = buyTower(Button.Name)
        print("Checking sucsess")
        if sucsess then
            Button.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
            return true
        else
            return false
        end
    end)
end

which will never "stop", you have to use somethings like

--outside function, to parse variable
local btn

--inside purchase function
if btn then
  btn:Disconnect()
end
btn = SkillTreeFrame.Stats.Pruchase.Activated:Connect(function() ...
-- rest of code

Hope you understand what i mean

hushed basin
#

Yeah, I figured I needed to break out somehow

#

I just didnt know how, Ill try to add this, thanks!

#
local btn = nil

function purchase(Button)
    if btn then
        btn:Disconnect()
    end
    btn = SkillTreeFrame.Stats.Pruchase.Activated:Connect(function()
        SkillTreeFrame.Stats.Purchase.Activated:Connect(function()
            local sucsess = buyTower(Button.Name)
            print("Checking sucsess")
            if sucsess then
                Button.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
                return true
            else
                return false
            end
        end)
    end)
end
#

This?

slender sapphire
#

Yea looks good to me

hushed basin
#

alr thx

#

Il tryit

#

13:19:18.160 â–¶ Checking sucsess (x36) - Client - SkillTreeScript:95

#

Still fires multiple times :/

slender sapphire
#

Thats looks strange, can you debug if the disconnect being triggered?

hushed basin
#

yeah

#

ill add prints n stuff

slender sapphire
#

Wait, you have 2 Activated:Connect()? keeek

#
btn = SkillTreeFrame.Stats.Pruchase.Activated:Connect(function()
        SkillTreeFrame.Stats.Purchase.Activated:Connect(function()
hushed basin
#

uhh yeah

#

oh crap

#

lol

#

13:27:20.892 Activated - Client - SkillTreeScript:130
13:27:20.893 Stats Visible - Client - SkillTreeScript:133
13:27:20.893 Puchase Fired - Client - SkillTreeScript:89

#

I click the green button once

#

13:27:36.142 Activated - Client - SkillTreeScript:130
13:27:36.142 Stats Visible - Client - SkillTreeScript:133
13:27:36.142 Puchase Fired - Client - SkillTreeScript:89
13:27:36.142 btn = smthn - Client - SkillTreeScript:91

#

I click a dif one

#

13:27:47.891 1 - Client - SkillTreeScript:95
13:27:48.491 3 - Client - SkillTreeScript:97
I click purchase

#

I think its working

slender sapphire
#

Nice. Glad i could help you, hopefully? xD

hushed basin
#

It works!

#

Thanks!

#

Ive been stuck on this for a while!

slender sapphire
#

Good luck on your progression

hushed basin
#

Hey, sry to ask again...

#

but I got a similar problem if u can help...

#
UserInputService.InputBegan:Connect(function(input, processed)
    if processed then
        return
    end

    local clones = inventoryFrame.ScrollingFrame:GetChildren()

    for i, clone in ipairs(clones) do
        
        if clone:IsA("Frame") and clone.Name ~= "Template" then
            clone.Image.Activated:Connect(function()
                if input.UserInputType == Enum.UserInputType.MouseButton1 then
                    local equipped = equipFunc:InvokeServer(clone.Name)
                    if equipped == 1 then
                        clone.EquipedText.Text = "✅"
                    elseif equipped == 2 then
                        clone.EquipedText.Text = ""
                    end
                elseif input.UserInputType == Enum.UserInputType.Touch then
                    local timeSinceLastTouch = tick() - lastTouch
                    if timeSinceLastTouch <= 0.25 then
                        local equipped = equipFunc:InvokeServer(clone.Name)
                        if equipped == 1 then
                            clone.EquipedText.Text = "✅"
                        elseif equipped == 2 then
                            clone.EquipedText.Text = ""
                        end
                    end
                    lastTouch = tick()
                end
            end)
            
            break
        end
    end
end)
#

its overloading the datastore again :/

#

idk why it fires so many times, but I tried to add a break but I dont think that really does anything :/

slender sapphire
#

Im not familiar with datastore.
As i see, if you press any buttons, or mouse click, it will clones everytime? modcheck

#

Which will also create, very many, times triggers.

hushed basin
#

So uhh

#

Ill make a vid

slender sapphire
#

Im not excatly sure what you are trying there, but i wouldn't use somethings like the script you made.

hushed basin
#

Make an inventory system

#

and an equip system

#

The server handles all of the equipment, I just need the client to change visuals based on if its equiped or not

#

So I think that the client sends 2 requests at the same time?

#

ill compress and send the vid

slender sapphire
#

Okay, i'll try to explain what happens in the script.
Let's say, you logged in, you going to click right click, It will fire the script which will get throught to the inventory childern and then create a clone.Image.Activated. (When you rightclick for first time, that will be fine)
But let's say, you will now move around char, Which will trigger the script again and create another clone.Image.Activated.

So you shouldn't really use UserInputService.InputBegan:Connect(function()

hushed basin
#

How would I achive the goal instead of using that?

#

Cause I thought that was the problem

#

cause it would fire EVREY time I click ect

#

Like is there a way to see if the player activated the button?

#

but then check for a double tap?

#

Cause if the player on mobile scrolls there guna hit buttons

#

Heres what It looks like

#

just ignore the output for now ig

slender sapphire
#

Yes, excatly, it will fire everytime and create, very much triggers.

I just kinda need more content, what you are trying to do?

I would do for example, remove the UserINputService, Just only the rest code

local clones = inventoryFrame.ScrollingFrame:GetChildren()

    for i, clone in ipairs(clones) do
        
        if clone:IsA("Frame") and clone.Name ~= "Template" then
            clone.Image.Activated:Connect(function()
                if input.UserInputType == Enum.UserInputType.MouseButton1 then
                    local equipped = equipFunc:InvokeServer(clone.Name)
                    if equipped == 1 then
                        clone.EquipedText.Text = "✅"
                    elseif equipped == 2 then
                        clone.EquipedText.Text = ""
                    end
                elseif input.UserInputType == Enum.UserInputType.Touch then
                    local timeSinceLastTouch = tick() - lastTouch
                    if timeSinceLastTouch <= 0.25 then
                        local equipped = equipFunc:InvokeServer(clone.Name)
                        if equipped == 1 then
                            clone.EquipedText.Text = "✅"
                        elseif equipped == 2 then
                            clone.EquipedText.Text = ""
                        end
                    end
                    lastTouch = tick()
                end
            end)
            
            break
        end
    end
fringe salmonBOT
#

studio** You are now Level 12! **studio

hushed basin
#

Yeah thats what I did

#

But I need to figure out if the player double clicked on mobile

#

is there any way to do this?

#
local clones = inventoryFrame.ScrollingFrame:GetChildren()

for i, clone in ipairs(clones) do    
    if clone:IsA("Frame") and clone.Name ~= "Template" then
        clone.Image.Activated:Connect(function()
            if input.UserInputType == Enum.UserInputType.MouseButton1 then
                local equipped = equipFunc:InvokeServer(clone.Name)
                if equipped == 1 then
                    clone.EquipedText.Text = "✅"
                elseif equipped == 2 then
                    clone.EquipedText.Text = ""
                end
            elseif input.UserInputType == Enum.UserInputType.Touch then
                local timeSinceLastTouch = tick() - lastTouch
                if timeSinceLastTouch <= 0.25 then
                    local equipped = equipFunc:InvokeServer(clone.Name)
                    if equipped == 1 then
                        clone.EquipedText.Text = "✅"
                    elseif equipped == 2 then
                        clone.EquipedText.Text = ""
                    end
                end
                lastTouch = tick()
            end
        end)
        
        break
    end
end
hushed basin
slender sapphire
hushed basin
#

thx

slender sapphire
#

question, since i never testing around with "mobile" things.

will clone.Image.Activated:Connect() triggered, when user click and hold on it?

hushed basin
#

what

#

Do u mean if I use that will it fire when somone click and holds?

#

I think the present function contains an input value

#

im going to mess around with that

slender sapphire
#

Not sure if this gonna works, but my guess is:

clone.Image.Activated:Connect(function()
  UserInputService.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
      --rest code
    elseif input.UserInputType == Enum.UserInputType.Touch then
      --rest code
    end
  end)
end)
hushed basin
#

ok so reverse it

#

ill try that

#

So uh

#

I cant even click on them

#

Is it because of the scrolling frame?

slender sapphire
#

No, i assume it won't work that way.

hushed basin
#

crap

#

Ok ima fix that

#

then try the function because it wont do any good if I cant click the buttons no more

slender sapphire
#

IMma try somethings on studio, instead asking you to check somethings xD

hushed basin
#

alr, thx

#

OK so now im REALLY confused

#

Its not even activating a print when I click it

slender sapphire
hushed basin
#
local clones = inventoryFrame.ScrollingFrame:GetChildren()

for i, clone in ipairs(clones) do    
    if clone:IsA("Frame") and clone.Name ~= "Template" then
        clone.Image.Activated:Connect(function()
            print("Activated")
            UserInputService.InputBegan:Connect(function(input)
                if input.UserInputType == Enum.UserInputType.MouseButton1 then
                    local equipped = equipFunc:InvokeServer(clone.Name)
                    if equipped == 1 then
                        clone.EquipedText.Text = "✅"
                    elseif equipped == 2 then
                        clone.EquipedText.Text = ""
                    end
                elseif input.UserInputType == Enum.UserInputType.Touch then
                    local timeSinceLastTouch = tick() - lastTouch
                    if timeSinceLastTouch <= 0.25 then
                        local equipped = equipFunc:InvokeServer(clone.Name)
                        if equipped == 1 then
                            clone.EquipedText.Text = "✅"
                        elseif equipped == 2 then
                            clone.EquipedText.Text = ""
                        end
                    end
                    lastTouch = tick()
                end
            end)
        end)
        
        break
    end
end
#

it doesnt print???

slender sapphire
#

Okay, i assumed, you are creating the "Shop" over script?

hushed basin
#

So the first thing like 2 hours ago was the "purchase shop"

#

now this func is the equip func

#

different thing

#

but esentally yes

slender sapphire
#

The things is, This script might be loading first, so he see that there is nothing than beside Template in GetChildren()

#

Okay, no worries, we need to change a little bit here then

hushed basin
#

yeah right

#

so ill load it after they load it?

#

I can send the full script rq

#
local inventoryFrame = script.Parent:WaitForChild("Inventory")
local shopButton = script.Parent:WaitForChild("OpenShop")
local loadInventoryEvent = game:GetService("ReplicatedStorage"):WaitForChild("LoadInventory")
local equipFunc = game:GetService("ReplicatedStorage"):WaitForChild("EquipFunc")
local UserInputService = game:GetService("UserInputService")
local lastTouch = tick()

shopButton.Activated:Connect(function()
    inventoryFrame.Visible = not inventoryFrame.Visible
end)

loadInventoryEvent.OnClientEvent:Connect(function(inventory, loadout)
    for tower, hasTroop in  pairs(inventory) do
        if hasTroop.HasTroop == true then
            local towerInReplicatedStorage = game.ReplicatedStorage.Towers:FindFirstChild(tower)
            if towerInReplicatedStorage then
                if not inventoryFrame.ScrollingFrame:FindFirstChild(tower) then
                    local clone = inventoryFrame.ScrollingFrame.Template:Clone()
                    clone.Parent = inventoryFrame.ScrollingFrame
                    clone.Image.Image = towerInReplicatedStorage["0"].Render.Value
                    clone.PriceText.Text = towerInReplicatedStorage["0"].Cost.Value
                    clone.Name = tower
                    clone.Visible = true
                end    
            end
        end
    end
end)
#

thats the top 1/2

slender sapphire
#

That's fine, we will just changed the "event".

Also i figured out with the if user is at mobile or not:

scrollingFrame.ChildAdded:Connect(function(clone)
    if clone:IsA("Frame") and clone.Name ~= "Template" then
        clone.Image.Activated:Connect(function()
            if UserInputService.TouchEnabled then
                -- Mobile
            else if UserInputService.KeyboardEnabled then
                -- PC
            end
        end)
    end
end)
hushed basin
#

Could I do it like this?

#

uhh

fringe salmonBOT
#

studio** You are now Level 7! **studio

hushed basin
#

It wont let me send???

#

WHAT

#

bro

#

Can u acept my friend rq

#

its broken