How to check if a player own's a tool

Before i start. I gotta say that im not talking about gamepasses. im talking about in-game tool’s, and what im trying to achieve is. Let’s say the player has 200 something, and for that 200 something he can buy a tool. But when he rejoins he doesn’t have it. How do i fix that?

1 Like

Use DataStores to store data and so when they rejoin that data will be stored and you can reload it. Im ngl i tried using datastores and my brain could not handle it. just have a few bool values like ownstool(whatever) and if its true then they own it and you can store the value of the bool value in the data store and when you load it you can see if they have the tool

This will help you. Datastores are data that are persistent across games or live sessions.
After reading please make sure you make two datastores; one for test data, and one for production data.
Production data will be the ones players use. Test data will be the ones the developers use. Why we need to separate them is because players may be confused when their saved data changes due to the fact the developers had modified the data in developing.

Happy developing!

uhhhh im not trying to be annoying or something but i kind ooooooooooooooooooooof dont understand data stores. it just gives me an error in my brain whenever i read the data stores documentation.

I should probably explain what it is then.

Datastores are enabled through a service you can enable through the game settings.
How datastores work is essentially you can create and delete databases and create data within the databases.

Say a player gets an item and we want to save it to give it to them when joining back (which is your scenario).
We first create a database with :GetDataStore(). The arguments to the function is one; a string. The string is the name of your datastore. Usually the first time you get a datastore it automatically creates it and then from that point on it just refers to it.

Example:

local dataStoreService = game:GetService("DataStoreService")
local itemStore = dataStoreService:GetDataStore("Items")

This is our first datastore. As you can see we’re calling DataStoreService and making a datstore called "Items".
Now we want to get data to that datastore. How? Like so:

local success, hasItem = pcall(function()
   return itemStore:GetAsync("User1234")
end)

This gets the data associated with the key "User1234". Keep in mind we’d usually store player IDs instead.
Why we use a pcall() is because there may be errors getting it. We can tell the player that it failed to save like so:

if not success then
   -- Warn player here
end

There is also :SetAsync(). Likewise you’d also need a pcall() because there can be errors saving too.

local success, err = pcall(function()
   itemStore:SetAsync("User1234", {"Item name here", true})
end)

We’d use a table because we also want to store the item name, so for example we know the player has either a Bow or an Apple.

Now put this under a CharacterAdded event:

local success, hasItem


success, hasItem = pcall(function()
   return itemStore:GetAsync("User1234")
end)
repeat
   success, hasItem = pcall(function()
      return itemStore:GetAsync("User1234")
   end)
until success
if hasItem[2] then
   game.ServerStorage[hasItem[1]]:Clone().Parent = game.Players[playerId]
   -- in this context playerId would be "User1234"
end

and that’s how you figure out how a player has an item.
Say he goes to a shop and he buys an item, likewise we’d do:

local sucess, err
success, err = pcall(function()
   return itemStore:SetAsync("User1234", {"Item name here", true})
end)

repeat
   success, err = pcall(function()
      return itemStore:SetAsync("User1234", {"Item name here", true})
   end)
until success

Why we want to repeat is so we don’t have to make an annoying popup for the player every time he fails. You may add a timeout for this to make a popup when it doesn’t succeed within some amount of tries.

Keep in mind I actually didn’t test all of this and this is just me reading the docs without going into studio lololol just tell me if there’s any problems

1 Like

Yo so about the

local success, hasItem


success, hasItem = pcall(function()
   return itemStore:GetAsync("User1234")
end)
repeat
   success, hasItem = pcall(function()
      return itemStore:GetAsync("User1234")
   end)
until success
if hasItem[2] then
   game.ServerStorage[hasItem[1]]:Clone().Parent = game.Players[playerId]
   -- in this context playerId would be "User1234"
end

do i put at the start something like “game.Players.PlayerAdded:Connect(function(character)
character.CharacterAdded:Connect(function()” ?

Yo thanks it actually worked!1!11

That’s exactly what happened to me. Glad to see I’m not the only one that was having a problem.

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