How would I give player money with Profile Service?

I use the template provided on the profile service API with some changes: Basic Usage - ProfileService

-- ProfileTemplate table is what empty profiles will default to.
-- Updating the template will not include missing template values
--   in existing player profiles!
local ProfileTemplate = {
	Cash = 0,
	Gems = 0,
	Batteries = 0,
}

----- Loaded Modules -----

local ProfileService = require(game.ServerScriptService.ProfileService)

----- Private Variables -----

local Players = game:GetService("Players")

local ProfileStore = ProfileService.GetProfileStore(
	"PlayerData",
	ProfileTemplate
)

local Profiles = {} -- [player] = profile

----- Private Functions -----

local function GiveCash(profile, amount, val)
	-- If "Cash" was not defined in the ProfileTemplate at game launch,
	--   you will have to perform the following:
	if profile.Data.Cash == nil then
		profile.Data.Cash = 0
	end
	-- Increment the "Cash" value:
	profile.Data.Cash = profile.Data.Cash + amount
end


--[[

local function DoSomethingWithALoadedProfile(player, profile)
	profile.Data.LogInTimes = profile.Data.LogInTimes + 1
	print(player.Name .. " has logged in " .. tostring(profile.Data.LogInTimes)
		.. " time" .. ((profile.Data.LogInTimes > 1) and "s" or ""))
	GiveCash(profile, 100)
	print(player.Name .. " owns " .. tostring(profile.Data.Cash) .. " now!")
end

]]


local function PlayerAdded(player)
	local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId)
	if profile ~= nil then
		profile:AddUserId(player.UserId) -- GDPR compliance
		profile:Reconcile() -- Fill in missing variables from ProfileTemplate (optional)
		profile:ListenToRelease(function()
			Profiles[player] = nil
			-- The profile could've been loaded on another Roblox server:
			player:Kick()
		end)
		if player:IsDescendantOf(Players) == true then
			Profiles[player] = profile
			-- A profile has been successfully loaded:
			-- DoSomethingWithALoadedProfile(player, profile)
		else
			-- Player left before the profile loaded:
			profile:Release()
		end
		local DataLoaded = Instance.new("StringValue",player)
		DataLoaded.Name = "DataLoaded"
	else
		-- The profile couldn't be loaded possibly due to other
		--   Roblox servers trying to load this profile at the same time:
		player:Kick() 
	end
end

----- Initialize -----

-- In case Players have joined the server earlier than this script ran:
for _, player in ipairs(Players:GetPlayers()) do
	task.spawn(PlayerAdded, player)
end

----- Connections -----

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(player)
	local profile = Profiles[player]
	if profile ~= nil then
		profile:Release()
	end
end)

How would I give a player, for example, 10 cash if they pick up a coin?
Also how would I get the players current amount of each value?

Well here is an example: Developer Products - ProfileService

2 Likes

Is there a way to give the player money form another script and not the main script? Like a script in a coin?

You can use BindableEvents that when a coin is picked up, it fires a bindable event to add the cash.

Example:
ProfileService script:

BindableEvent.Event:Connect(function(plr, amount)
    Profiles[plr].Cash += amount
end)

Coin script:

Coin.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
       BindableEvent:Fire(plr, 10) -- example
       Coin:Destroy()
    end
end)

Just of course make sure to set BindableEvent as the bindable event

2 Likes

Yeah like this:

local ProfileService = require(game.ServerScriptService.ProfileService)
local GameProfileStore = ProfileService.GetProfileStore(
	"PlayerData",
	SETTINGS.ProfileTemplate
)
local profile = GameProfileStore:LoadProfileAsync("Player_" .. player.UserId)

profile.Data.Cash += 100
2 Likes

would this be the same as using a remote event except just on the server?

I get this error.

ServerScriptService.Script:40: attempt to perform arithmetic (add) on nil and number
local gc : BindableEvent = game.ServerScriptService.gCash

gc.Event:Connect(function(plr, amount)
	Profiles[plr].Cash += amount -- line 40
	print(Profiles[plr].Cash)
end)

managed to fix it thank you for the help :smiley:

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