#not incrementing cant see what im doing wrong probs missing something

1 messages · Page 1 of 1 (latest)

ornate raft
#
... --Code
fireClientGui.OnClientEvent:Connect(function(ItemName, ItemCategory, itemID)
    local Stack = 0
        --[[ --Not Related--
    local invFrame = Inventory.Menus.InvFrame
    local All = invFrame.ScrollingFrameAll.ItemHolder
    local Furniture = invFrame.ScrollingFrameFurniture.ItemHolder
    local Shapes = invFrame.ScrollingFrameShapes.ItemHolder
    local item = game.ReplicatedStorage.Parts.ItemFrames[ItemName]
    local itemCopy = item:Clone()
    itemCopy.Name = ItemName
        --]]
    
    for _, v in pairs (game.Players.LocalPlayer.owneditems:GetChildren()) do
        print(v.Name)
        print(itemID)
        if v.Name == itemID then
            Stack += 1
        end
    end
    print(Stack)
... --More Code

--This is the output i get
1  -  Client - ItemInvScript:25
1  -  Client - ItemInvScript:26
0  -  Client - ItemInvScript:31

So basically i have an inventory system.
Each item is referenced by an item ID - in this case:
ItemName = Cube,
ItemCategory = Shape,
itemID = 1,

The name and category isn't important - but it will store the item by ID number not name.

It SHOULD increment since the output shows that they both the same but 'Stack' will not increment for some reason

oak tapir
# ornate raft ```lua ... --Code fireClientGui.OnClientEvent:Connect(function(ItemName, ItemCat...

It'll reset because of variable scope. When the function for OnClientEvent is ran, that Stack variable will be discarded, because the function it was in is no longer being used.

So move local Stack = 0 outside of the function, above the connect.

https://create.roblox.com/docs/luau/scope

The scope of a variable or function is the block of code that can access it.

ornate raft
#

i did earlier and it still wouldn't increment tho i will try again ive been wrong before XD

#

i moved it inside because the 'print(stack)' is still inside the event function

#

it still wont increment sadly i put it outside the function - in my case i do want it inside the function because i want the stack to reset to 0 every time i call it because i dont know what item ill be buying

#

also for context - this is a gui localscript

#

it just updates the GUI to show the amount of the item i 'clicked'

oak tapir
#

Can you show me what you have after the changes

ornate raft
#

here ill send screenshot of full code and the output

#

hold on

#

after moving it outside the scope

#

and same thing inside aswell

oak tapir
ornate raft
#

sorry my code is super messy i do most of this stuff without looking up tutorials

#

right at the top is the only place

#

i want it to be a intvalue basically that starts at 0

#

i could just set an actual "IntValue"

#

inside the workspace or something but i thought it wouldof been fine in a script since i want it to reset to 0 every time i call the event

#

its only purpose is to read the amount of times the 'specific item' is in the folder and increment it so i can set the stack amount inside the GUI

#

here a good example of what it looks like hold on

oak tapir
#

How are these scripts being used? It should never be resetting the variable, unless you're creating new scripts every time rather than re-using the one script

ornate raft
#

i call the event from a regular server script inside cube (its not actually - but for simplicity lets say it is)
that calls the localscript inside starterGUI
the Stack variable is created inside the GUI localscript
the Stack variable should be 0
the stack variable then increments by however many items called '1' is inside the itemowned folder
then the inventory text is = stack at the bottom of the script (couldnt fit it into screenshot)

#

oops sent too early ill edit my message

#

hold on

#

that number (frame.stacknumber.Text) is = Stack

#

then the stack will be set back to 0 if i call the event again ( well i think it should )

next remnantBOT
#

studio** You are now Level 4! **studio

oak tapir
#

That's odd, I have no idea why the Stack variable would be resetting