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?
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.
script.Parent
will return the wrong instance which causes the code to NOT work.SubtractVal
a NumberValue
or IntValue
? if not then returned value will be an invalid type.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.