Tool script giving players random tools for free?

Hey, i’m currently making a script that saves a player’s tools using datastores and caching. It randomly gives players gamepass tools and other tools that they don’t even own, this doesn’t happen to me in studio playtesting, nor in game which makes it harder to debug. Please help me!

The script is finished! thanks to anyone that helped.

What seems to be the problem is that you aren’t actually giving each player a copy of defaultToolsList, but instead you’re directly passing and setting values in that main table for each player.

This means that any player who joins and has a gamepass/badge will cause the Owned flag to be set for all future players who join. The easiest way to fix this is by creating a copy of defaultToolsList in loadData for each new player. (This would also explain why it isn’t happening to you solo.)

I actually recently stumbled across my own issue similar to this, and found this article to be very helpful, and it can be applied to this problem as well. Instead of referencing defaultToolsTable directly, add a function to clone it, and replace all references to the original table with the cloned one.

local function deepCopy(inp)
    local out = {}
    for key, value in pairs(inp) do
        if type(value) == "table" then
            value = deepCopy(value)
        end
        out[key] = value
    end
    return out
end

...

local toolsListClone = deepCopy(defaultToolsList)
Cache[plr.UserId] = toolsListClone
plrData = toolsListClone

...
2 Likes

Thank you for helping me! This script used to use Datastore2, and that issue didn’t happen at all.
All I did in this script was use caching, and roblox’s datastores.