#Help - variable not incrementing?

1 messages · Page 1 of 1 (latest)

light wind
#
local item = game.Workspace.Cube
local ClickDetector = item:WaitForChild("ClickDetector")
local fireclientgui = game.ReplicatedStorage.Events.InventoryEvents.FireClientGui
local firepurchasegui = game.ReplicatedStorage.Events.InventoryEvents.FirePurchaseGui
local itemList = require(game.ServerScriptService.Inventory.ItemsModule)
local itemID = item:GetAttribute("ItemID")
local ItemName = itemList[itemID].Name
local ItemPrice = itemList[itemID].Price
local ItemCategory = itemList[itemID].Category
local DataManager = require(game.ServerScriptService.DataStore.Data.DataManager)

ClickDetector.MouseClick:Connect(function(player)
    firepurchasegui:FireClient(player, ItemName, ItemPrice)
end)

firepurchasegui.OnServerEvent:Connect(function(player)
    local Stack = 0

    if player.leaderstats.Coins.Value < ItemPrice then
        player.PlayerGui.Inventory.PurchaseFail.Visible = true
        task.wait(2)
        player.PlayerGui.Inventory.PurchaseFail.Visible = false
    else        
        DataManager.removeCoins(player, ItemPrice)
        
        local ItemCreate = Instance.new("NumberValue")
        ItemCreate.Name = itemID
        ItemCreate.Parent = player.owneditems

        --IMPORTANT PART HERE--
        for _, v in pairs (player.owneditems:GetChildren()) do
            print(v.Name)                                       --line 31
            print(itemID)                                       --line 32
            if v.Name == itemID then
                Stack += 1
            end
        end
        
        print(Stack)                                            --line 37
        --]]

        ItemCreate.Value = Stack
        fireclientgui:FireClient(player, ItemName, ItemCategory, itemID, Stack)
        player.PlayerGui.Inventory.PurchaseSuccess.Visible = true
        task.wait(2)
        player.PlayerGui.Inventory.PurchaseSuccess.Visible = false
    end
end)
#

The Output when firepurchasegui event is called:
1 - Server - InventoryHandler:31
1 - Server - InventoryHandler:32
0 - Server - InventoryHandler:37

if v.Name is equal to itemID then increment Stack by 1
Stack does not increment
.

#

The Output when called twice:
1 - Server - InventoryHandler:31
1 - Server - InventoryHandler:32
1 - Server - InventoryHandler:31
1 - Server - InventoryHandler:32
0 - Server - InventoryHandler:37

Stack should print '2' (line 37)

#

please help

gleaming mauve
#

you got to give more detail

light wind
#

sorry i didnt mean to ask in someone elses thread

gleaming mauve
#

ok nvm i just need to read it

#

well you did, but its whatever

#

ill read

#

yeah because Stack is reset to 0 every time you call the event

light wind
#

thats fine i want it to be 0

gleaming mauve
#

oh right, i see

#

send the place file if it's small, it'll be easier

#

how many items are actually in player.ownedItems?

rose terrace
light wind
#

let me recreate it in another place cuz its in a big game rn

rose terrace
#

what game

gleaming mauve
#

sure

rose terrace
#

are you making

light wind
#

this is an inventory system

gleaming mauve
#

actually don't recreate it yet

#

lets bug test

#

add a print after your v.Name == itemID to see if it gets past there

light wind
#

okayy

#

no it didnt print

gleaming mauve
#

print what the itemID is at the start, and then what it is that you're actually comparing it to

#

to get a better picture of what's going on

light wind
#

here i actually cloned this post because the first person to help me was not understanding me but i have a bunch of images that will help

#

the itemID is 1

#

hold on

woven saddle
#

Wait

obtuse rootBOT
#

studio** You are now Level 3! **studio

woven saddle
#

is thisa local script?

light wind
#

was

obtuse rootBOT
#

studio** You are now Level 5! **studio

light wind
#

but i chucked it into a regular script and still broken

#

this is old photo

woven saddle
#

in the click detector?

gleaming mauve
#

if v.Name == tostring(itemID) then

#

try that ^

#

maybe an issue with numbers vs strings

light wind
#

okk yeah when i hover over both its v.Name: any and itemID: any

#

njot sure if that means much

woven saddle
#

yk what i usually do?

gleaming mauve
#

try it

woven saddle
#

I onetime got stuck with this for hours

#

when i wasnt as good with scripting

#

when i made an amusement park I put in in starterplayerscripts THEN fired to serverscriptservice as an event -

`local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
--Variables

---Main Client For Starting Sunburst Express
game.Workspace:WaitForChild("StartTestRide").ClickDetector.MouseClick:Connect(function()
local Speed = game.Workspace.RideTweenScript.Speed
game.ReplicatedStorage.StartRide1:FireServer(Speed.Value)
end)`

light wind
gleaming mauve
#

fixed it?

woven saddle
#

works?

light wind
#

it actually works thankyou

gleaming mauve
#

banger

light wind
#

yes

woven saddle
#

oh vcool

gleaming mauve
#

yeah, just an issue of types

#

annoying one to debug tbf

light wind
#

thanlkyouy so much man

gleaming mauve
#

beautiful

#

no prob bob

#

how long you been stuck on that?

light wind
#

lucky not too long about 2 days

gleaming mauve
#

dang still

#

anyways

light wind
#

since early yesterday

woven saddle
#

anyone wanna check out my github page

gleaming mauve
#

no brother

light wind
#

you know i actually did try
local name = tostring(itemID)
v.Name == name

#

and that didnt work

gleaming mauve
#

really?

light wind
#

so yeah scripting really picky sometimes

gleaming mauve
#

seems like it should

#

unless you did it the other way around?

light wind
#

this was back on localscript tho

gleaming mauve
#

¯_(ツ)_/¯

light wind
#

yeah i think ur right

woven saddle
gleaming mauve
#

mustve been the other way around yeah

#

thats a shame, you were so close to solving it then

#

lol

light wind
woven saddle
#

I used to think Roblox studio was just not working

light wind
#

something liek that

gleaming mauve
#

ryan why are you here

woven saddle
#

idk

#

I've been helping other people

light wind
#

i was also reading over other peoples scripts to see if i could help while waiting

#

which was why i was in the other thread

#

cheers again lightlim youre a legend

gleaming mauve
#

no prob bob