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

how to do custom purchases with leaderstats?

so basically I have a data store leaderstats script and it works but i'm trying to have the player be able to purchase a certain amount of something. so basically I want the player to click a button to confirm and then their cash stays the same exept subtracted by a value in replicated storage. heres what I have tried

-- code for the button script.Parent.MouseButton1Click:Connect(function() game.ReplicatedStorage.RemoteEvent:FireServer() end)

--script in serverscriptstorage game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr) plr.leaderstats.Cash.Value -= game.ReplicatedStorage.SubtractVal.Value end)

--leaderstats code local datastores = game:GetService("DataStoreService") local store = datastores:GetDataStore("CashSave")

game.Players.PlayerAdded:Connect(function(plr) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = plr

local Cash= Instance.new("NumberValue")
Cash.Name = "Cash"
Cash.Value = store:GetAsync(plr.UserId) or 0
Cash.Parent = leaderstats


local market = game:GetService("MarketplaceService")
market.ProcessReceipt = function(info)
	local userid = info.PlayerId
	local productid = info.ProductId
	local plr = game.Players:GetPlayerByUserId(userid)
	local leaderstats = plr.leaderstats
	local coins = leaderstats.Cash
	if productid == 1332434809 then
		coins.Value += 10
	elseif productid == 1332435305 then
		coins.Value += 50
	elseif productid == 1332435302 then
		coins.Value += 100
	elseif productid == 1332435304 then
		coins.Value += 250
	elseif productid == 1332435303 then
		coins.Value += 500
	elseif productid == 1332435306 then
		coins.Value += 1000
	elseif productid == 1332435307 then
		coins.Value += 5000
	elseif productid == 1332435301 then
		coins.Value += 10000
	end
end

end)

game.Players.PlayerRemoving:Connect(function(Plr) store:SetAsync(Plr.UserId,Plr.leaderstats.Cash.Value) end)

Can anyone help?

1 answer

✓ Accepted answer

After reading the question, I don't seem to understand what you're actually asking and the question title doesn't help either. The code formatting that you gave is also anything but helpful which forces me to essentially reconstruct the code.

But after reconstructing the code, it seems the code work as intended so I don't understand the problem clearly so here's the list of potential problems because this question is vague and there's no error message to work with.

  1. Is the script used for TextButton a LocalScript? If not then you need to replace it with LocalScript or Script with RunContext of Client (https://create.roblox.com/docs/reference/engine/classes/LocalScript)
  2. Is the button script parented to TextButton? If not then script.Parent will return the wrong instance which causes the code to NOT work.
  3. Is the SubtractVal a NumberValue or IntValue? if not then returned value will be an invalid type.
  4. Is the TextButton actually a TextButton? (like TextBox having the name of "TextButton") If not then you need to replace it with the actual TextButton
  5. Are you using Studio to run the game? If yes then refer to 5.1

5.1. Did you turn the "Enable Studio Access to API Services" on? If not then you need to turn it on for the code to work in the studio*

*: WARNING: NOT RECOMMENDED TO ENABLE BECAUSE PLAYERS' SAVED DATA CAN BE OVERWRITTEN, ONLY ENABLE THIS FOR TESTING PLACES WHERE THERE ARE NO PLAYERS ACTIVELY PLAYING

If you find this answer helpful or working then please accept the answer.

2 comments

0

I think he's trying to say how to make a purchase system that will remove his cash

0

I could be wrong

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