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

My DataStore it's saving Values, But When Player Buys Weapons, it doesn't?

So basically, there is a goldbar system, where u click it, it gives u a point (franc) It works perfectly fine, but when i try to buy a gun, and it removes 10 Francs from my system, it works, it removes 10, but when i reconnect from the game, the -10 francs, get back into my inventitory as i never bought the AK-47

**ServerScript: **

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

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder", plr) --
    leaderstats.Name = "leaderstats" -- 
    Francs = Instance.new("IntValue", leaderstats) 
    Francs.Name = "Francs" 
	Francs.Value = 0

    local key = "plr-"..plr.UserId
    local Saved 
	
    local success, errorMessage = pcall(function()
           Saved = DataStore:GetAsync(key)
    end)


   if success then
        if Saved then
            Francs.Value = Saved[1] 
        else
            local ValuesToSave = {Francs.Value}
            DataStore:SetAsync(key, ValuesToSave)
        end
else
    warn(errorMessage)
 end
end)


game.Players.PlayerRemoving:Connect(function(plr)
    local key = "plr-"..plr.UserId
    local valuesToSave = {Francs.Value}
    DataStore:SetAsync(key, valuesToSave)
end)

Local Script (where u buy AK-47)

local player = game.Players.LocalPlayer
local AKremote = game.ReplicatedStorage.weaponsRemotes.AK


script.Parent.MouseButton1Click:Connect(function(click)
  if player.leaderstats.Francs.Value >= 10 then
     player.leaderstats.Francs.Value = player.leaderstats.Francs.Value - 10
	AKremote:FireServer(player)
    end
end)

4 comments

0

I see you are using a remote event. Is that event for removing 10 Francs from the players stats?

0

umm no, the remote only gives u the weapon

0

Ahh, you should make it take away the players stats too. If you don't it only happens client side, and your datastore only saves server side data

0

can u help me with that

1 answer

You can include this with your already previous script that inserts the AK into the characters backpack.

AKremote.OnServerEvent:Connect(function(player) -- the player is the person who fired the event
	local stats = player.leaderstats     
	stats.Francs.Value = stats.Francs.Value - 10
end)

You will only probably need the 2 lines in between to include in with the rest of your event script.

I hope this helps! :)

3 comments

0

It doesn't work still, i dont get any errors, it doesn't - my Francs

0

Ohh my bad. I put an addition sign. It’s supposed to be a minus sign. I’ll edit that right now

0

also, you'll want to remove the part in the local script that subtracts the cash

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