One time purchase to access team

hello, i made a script that if you have more than 1000 coins, it removes them and makes your char changes team, but the problem is that each time the player leaves he gotta pay again, is there a way to make it a one time purchase ?

local plr = game.Players.LocalPlayer
local part = script.Parent
local team = BrickColor.new("New Yeller")
part.Touched:Connect(function(h)
	local plr = game.Players:GetPlayerFromCharacter(h.Parent)
	if plr then
		if plr.leaderstats.Coins.Value >= 1000 then
		
			plr.leaderstats.Coins.Value -= 1000
			
		plr.TeamColor = team
		plr:LoadCharacter()
		else 
			print("poor")
			end
	end
end)
1 Like

If you’re saying to make the player be in the team that they bought everytime they rejoin. I suggest adding a value to them that has team name and determing which team should they be on because of value name. Then you must use Data Stores. Store the Values. And when they rejoin, the values will load in the team name they were in and use it to determine what team they should be in.

You need to use datastore for this.

1 Like

Yes, you can make it a one-time purchase by saving the player’s purchase status in DataStore or a similar storage system.

how could i do this? im not very experienced

I also explained vague at my first post to this.

you could add a bool value to the player and then when the player buys the item it will change the value to true

game.Players.PlayerAdded:Connect(function(player)
		local item = Instance.new("BoolValue")
		item.Name = "itemname"
		item.Parent = player -- you could save items in a folder aswell
end)

then add a datastore for when the player joins and leave it will save and load the value

how would i change this value from a local script?

Use remote events to change them

a remote event, otherwise it won’t read the client value and won’t save.

One way to make it so that the player only has to pay once for the team change would be to use some kind of data persistence to remember that the player has already purchased the team change.

One option is to use a Value on an object in the Roblox data store. You could create a DataStoreService object and then a DataStore object with a unique name for your game. Then, in the script you already have, you could use the Value from the data store to remember if the player has already purchased the team change.

Here is an example of how you could modify your script to work with the data store:

local DataStoreService = game:GetService("DataStoreService")
local TeamPurchaseDataStore = DataStoreService:GetDataStore("TeamPurchases")

local plr = game.Players.LocalPlayer
local part = script.Parent
local team = BrickColor.new("New Yeller")

part.Touched:Connect(function(h)
    local plr = game.Players:GetPlayerFromCharacter(h.Parent)
    if plr then
        local purchased = false
        local success, result = pcall(function()
            return TeamPurchaseDataStore:GetAsync(plr.UserId)
        end)
        if success then
            purchased = result or false
        end

        if not purchased and plr.leaderstats.Coins.Value >= 1000 then
            plr.leaderstats.Coins.Value -= 1000
            plr.TeamColor = team
            plr:LoadCharacter()

            -- Save that the player has already purchased the team change
            local success, result = pcall(function()
                TeamPurchaseDataStore:SetAsync(plr.UserId, true)
            end)
        elseif not purchased then
            print("poor")
        else
            -- If the player has already purchased the team change, just change their team
            plr.TeamColor = team
            plr:LoadCharacter()
        end
    end
end)

With this code, the script first checks if the player has already purchased the team change using the data store. If the player hasn’t bought the team change, it is checked if they have enough coins to buy it. If they have enough coins, the coins are deducted and the player’s team is changed. Then, it is saved in the data store that the player has bought the team change.

If the player has already purchased the team change, simply change his equipment without deducting coins.

This should be done in another script to reduce memory usage instead of 1 script or in ServerScriptService

That’s Correct.

Split the code into multiple scripts to reduce memory usage and increase the efficiency of the game. Because if it have too much code in a single script, it can slow down your game and increase memory usage.

Also suggests that move this code to a script in ServerScriptService. ServerScriptService is a folder in Roblox Studio where you can put scripts that run on the server instead of the client. This helps prevent cheating in the game and increases the security of the game.

1 Like

to save a value(s) you could use this script in serverscriptservice

note that datastore may not save in studio but will save in roblox

local DSS = game:GetService("DataStoreService")
local statsDS = DSS:GetDataStore("statsDS")

game.Players.PlayerAdded:Connect(function(player)
    local data
    
    local succ, err = pcall(function()
        data = statsDS:GetAsync(player.UserId.."-stats")
    end)
    
    if succ then
        print("success")
        if data then
            print("Got the data from the player datastore")
            item.Value = data[1] -- CHANGE ITEM TO YOUR VALUE
        end
    else
        print("There was an error while checking: "..player.Name)
        warn(err)
    end

end)
game.Players.PlayerRemoving:Connect(function(plr)
    local item = plr.item.Value -- CHANGE ITEM TO YOUR VALUE

    print("Got all the values")
    
    
    local data = {item }
    
    local succ, err = pcall(function()
        statsDS:SetAsync(plr.UserId.."-stats", data)
    end)
    
    if succ then
        print("Succesfully saved the data!")
    else
        print("There was an error while saving player leaderstats!")
        warn(err)
    end
end)