How can i make this only happen once?

I added a module to my game that gives the player a value to put in a folder when it checks successfully that the player has a gamepass owned, but this happens each time the player joins the server and i only want this to happen once, since each time the player joins it makes the new value and i only want 1 value of it to be inside that folder, this is the script
Screenshot 2024-08-23 100142

You can use either DataStoreService or HTTPService to make sure the game remembers if the player has it. If you are going for simplicity, go for DTS. If you are going for security, go for HTTP service. Im going to give a DTS example here, but ask me if you want an example for HTTP

local BoughtCartsDataStore = DataStoreService:GetDataStore('BoughtCartsDataStore')
function gamepassFunctions.OwnedUponJoining(plr: Player)
   local data
   local s,e = pcall(function()   
      data = BoughtCartsDataStore:GetAsync(plr.UserId)
   end)   if s and data then
      if not data['SantasSleigh'] then
         -- Add the string value
      end
   end
end
-- When Player leaving..
function gamepassFunctions.PlayerLeaving(plr : Player)
   local data = {
     ['SantasSleigh'] = plr.BoughtCarts.SantasSleigh
     -- Some other carts here if there are any
}
   local s,e = pcall(function()
      BoughtCartsDataStore:SetAsync(plr.UserId,data)
   end)
   if s then print('Success saving data!') else warn(e) end
end
1 Like

Would the http version block hackers from getting through the gamepass check without buying it?

What do you mean by a new value is created in the folder each time the player joins? When a player leaves the server, all children under the player instance are deleted, and this includes the BoughtCarts folder (and all of its children) that you have referenced.

However, assuming this still occurs, you would need to check if the value has already been created, and if so, ignore instancing it.

function gamepassFunctions.OwnedUponJoining(plr: Player)
    local boughtcarts = plr.BoughtCarts

    if not boughtcarts:FindFirstChild("SantasSleigh", true) then
        local newboughtcart = Instance.new("StringValue")
        newboughtcart.Name = "SantasSleigh"
        newboughtcart.Parent = boughtcarts
    end

end)
1 Like

im pretty sure the value doesnt get deleted upon joining since it only does it when its in the data store, like how the guy above added a data store leaving whereas it deletes it upon leaving and reloads it upon joining, but im not sure if its the case on why that happens since im still fairly new to roblox studio

Don’t you want it to create only once since the player started playing the game? If so then post #4 would create a value again once a player joins a server, seeing as they don’t have the value

Yes that is what i aimed for, i wanted the string value to not be added again and a check to be made to see if its in the players boughtcarts folder so it wouldnt make it again

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