Strange race condition and strange script behavior

Hello! Today i’ve been 5+ hours straight trying to debug this simple piece of code. It seems weird, as it should behave as scripted, but instead it makes an strange race condition where a line gets executed before another???

(Note: “Value” is either an empty table or a table containing a “Weapon” value.)


-- Just a signal that calls the function. Made with module "Signal". Maybe it's something wrong with the module??
DataService.UpdateSignal:Connect(function(Player, Key, Value) 

    if Key == "equipment" then

        -- Gets executed AFTER setting the value????
        print("Current equipment:", PlayersEquipment[Player]) 

        -- Sets the value but strangely it sets BEFORE the previous print??? I can't understand why...
        PlayersEquipment[Player] = Value 

        -- Behaves like normal
        print("New equipment:", PlayersEquipment[Player]) 
    end
        
end)

The previous code prints this:

When equipping something:
image

When unequipping something:
image

This is unexpected behavior, as it can’t scan the previous current equipment table to unequip anything that was equipped before…

STRANGE enough, when i call the function manually (a task.spawn instead from directly a Signal), the code works EXACTLY as expected

-- Function to update equipment from a player
local function EquipmentUpdateFunction(Player, Key, Value)
    if Key == "equipment" then
        print("Current equipment:", PlayersEquipment[Player])
        PlayersEquipment[Player] = Value
        print("New equipment:", PlayersEquipment[Player])
    end
end


-- SAME REMOTE, 
-- But instead calls a task spawn, 
-- which isn't ideal because to clean the equipment (unequip everything), 
-- it starts to happen the same as before...
DataService.UpdateSignal:Connect(function(Player, Key, Value)
    
    task.spawn(function()
        EquipmentUpdateFunction(Player, Key, Value)
        EquipmentUpdateFunction(Player, Key, {})
    end)

end)

image
(This is perfect, as it can detect the previously equipped items to unequip them. Sadly it’s manual…)

As you can see, setting manually the values makes it work. But it cannot be this way, as i need to have it connected directly from the update signal, to clean the equipment whenever it’s necessary!

Please help, as i’ve been for hours trying to debug this SIMPLE yet BUGGY code!

1 Like

Wdym by this?

Could you show the code which called the EquipmentUpdateFunction without task.spawn?

2 Likes

Well, i solved the problem. Aparently, using a RBXScriptSignal with a module called “Signal” will execute the code in a VERY STRANGE way. I don’t know what causes this, as it’s very weird!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.