Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a solution to my gamepass money script?

I have a regular script located in my ServerScriptService and I have a auto-saving money script that goes with it so it can save my money. The auto-saver works fine but I just need to find out why my money currency is not adding when a player purchases or purchased a gamepass.

Gamepass Script:

local player = script.Parent.Parent
local leaderboard = player:WaitForChild("leaderstats")
local currency1 = leaderboard:WaitForChild("Money")

local id = 1289535025
 
game.Players.PlayerAdded:connect(function(player)
    if game:GetService("GamePassService"):PlayerHasPass(player, id) then 
        print(player.Name .. " has the game pass!")
		currency1.Value = currency1.Value + 500
    else
        print(player.Name .. " doesn't have the game pass...")
    end
end)

Just in case: Auto-Saver script:

local DataStore = game:GetService("DataStoreService"):GetDataStore("Money")

game.Players.PlayerAdded:connect(function(player)
	local key = "key-"..player.UserId
	local folder = Instance.new("Folder",player)
	folder.Name = "leaderstats"
	local currency1 = Instance.new("IntValue",folder)
	currency1.Name = "Money"
	
	local save = DataStore:GetAsync(key)
	if save then
		currency1.Value = save[1]
	else
		local load = {currency1.Value}
		DataStore:SetAsync(key,load)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local key = "key-"..player.userId
	local load = {player.leaderstats:FindFirstChild("Money").Value}
	DataStore:SetAsync(key,load)
end)

Thanks!

1 answer

I'm no mad evil genius but I think your having a script crash. This basically means you have a local that is breaking a function. Try this script instead.

local leaderboard = player:WaitForChild("leaderstats")
local currency1 = leaderboard:WaitForChild("Money")

local id = 1289535025

game.Players.PlayerAdded:connect(function(player)
    if game:GetService("GamePassService"):PlayerHasPass(player, id) then 
        print(player.Name .. " has the game pass!")
        currency1.Value = currency1.Value + 500
    else
        print(player.Name .. " doesn't have the game pass...")
    end
end)

Also I wouldn't recommend using a gamepass for this, as players could just constantly rejoin and get cash. Look at Developer Products instead maybe.

This discussion is preserved as it was. New answers, comments, and votes are closed.