New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by
EEnergy8 158
9 days ago

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)
0
I see you are using a remote event. Is that event for removing 10 Francs from the players stats? DarkDanny04 310 — 9d
0
umm no, the remote only gives u the weapon EEnergy8 158 — 9d
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 DarkDanny04 310 — 9d
0
can u help me with that EEnergy8 158 — 9d

1 answer

Log in to vote
0
Answered by 9 days ago
Edited 9 days ago

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! :)

0
It doesn't work still, i dont get any errors, it doesn't - my Francs EEnergy8 158 — 9d
0
Ohh my bad. I put an addition sign. It’s supposed to be a minus sign. I’ll edit that right now DarkDanny04 310 — 9d
0
also, you'll want to remove the part in the local script that subtracts the cash DarkDanny04 310 — 9d
Ad

Answer this question