Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i fix my tool not saving the coins?

Asked by 3 years ago
Edited 3 years ago

So I'm creating a tool to give me plus 1 coin so I created a data store leader stats it works fine i tested it with the developer console but it doesn't save the tool coins here is the data store leaderstats :

local DataStore = game:GetService("DataStoreService")
local DS1 = DataStore:GetDataStore("CoinsDataStore")

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

    local Coins = Instance.new("IntValue",leaderstats)
    Coins.Name = "Coins"
    Coins.Value = 0

    Coins.Value = DS1:GetAsync(plr.UserId) or 0
    DS1:SetAsync(plr.UserId,Coins.Value)

    Coins.Changed:Connect(function(plr)
        DS1:SetAsync(plr.UserId,Coins.Value)
    end)
end)

Tool script:




-- Tool Script that give +1 click when clicking a tool local leaderstats = game.Players.LocalPlayer.leaderstats local Coins = leaderstats.Coins local Cooldown = false script.Parent.Activated:Connect(function() if Cooldown == true then return end if Cooldown == false then Cooldown = true Coins.Value = Coins.Value +1 wait(0.8) Cooldown = false end end)

It's not saving my coins from the tool when i click i do get +1 coins but it dosen't and i have not problem with my datastore what did i do that it's not saving what i get from the tools.

1 answer

Log in to vote
0
Answered by 3 years ago

I assume you're using a server script for this, if so, the value "LocalPlayer" is not replicated, this means only a local script can read it. If you're using a local script, use a server script or remote events (not reccomended, never trust the client.), a solution would be:

-- Creates empty variables to be edited later on. (This is a ServerScript)
local leaderstats 
local Coins
local Cooldown = false

local player
local character

script.Parent.Equipped:Connect(function() -- This will assign the values to the previously mentioned empty variables.

    character = script.Parent.Parent
    player = game.Players:GetPlayerFromCharacter(character)

    leaderstats = player:WaitForChild("leaderstats")
    Coins = leaderstats:WaitForChild("Coins")

end)

script.Parent.Activated:Connect(function() -- Once activated, change the value of Coins.
    if Cooldown == true then return end
    if Cooldown == false then
        Cooldown = true
        Coins.Value = Coins.Value +1
        wait(0.8)
        Cooldown = false
    end 
end)

Hope this helps!

Ad

Answer this question