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

In my game the taps is not getting registrar in the leaderstats but it is stuck on 12 help?

Asked by 3 years ago
Edited 3 years ago
local DataStoreService = game:GetService("DataStoreService")
local tapsStore = DataStoreService:GetDataStore("Taps")
local starterGui = game:GetService("StarterGui")

local replicatedStorage = game:GetService("ReplicatedStorage")
local UpdateRebirthGui = replicatedStorage.UpdateRebirthGui


local event = Instance.new("RemoteEvent")
event.Name = "AddStats"
event.Parent = game.ReplicatedStorage

event.OnServerEvent:Connect(function(plr)
    local taps = plr.leaderstats.Taps
    taps.Value = taps.Value +1
end)

game.Players.PlayerAdded:Connect(function(plr)
    --create leaderstats
    local is = Instance.new("Folder")
    is.Name = "leaderstats"
    is.Parent = plr
    local menu = Instance.new ("IntValue", is)
    menu.Name = "Taps"

    local taps = tapsStore:GetAsync("Player_"..plr.UserId)
    local success, errormessage = pcall (function()
         starterGui.TapGui.Frame.counter.Text = taps    
    end)
    if success then
        menu.Value = taps
    else
        warn(errormessage)
    end


end)

The error is between line 13-15

2 answers

Log in to vote
0
Answered by 3 years ago

You need to make the leaderstats FIRST before using the remote event. Programming languages moves from one line to the next. If the remote event tries to find leaderstats, it will result in nil because leaderstats do not exist yet. So you have to make the leaderstats first before doing the remote event.

game.Players.PlayerAdded:Connect(function(plr) --ALWAYS make leaderstats top priority.
    --create leaderstats
    local is = Instance.new("Folder")
    is.Name = "leaderstats"
    is.Parent = plr
    local menu = Instance.new("IntValue", is)
    menu.Name = "Taps"

    local taps = tapsStore:GetAsync("Player_"..plr.UserId)
    local success, errormessage = pcall(function()
         starterGui.TapGui.Frame.counter.Text = taps    
    end)
    if success then
        menu.Value = taps
    else
        warn(errormessage)
    end
end)

local event = Instance.new("RemoteEvent")
event.Name = "AddStats"
event.Parent = game.ReplicatedStorage

event.OnServerEvent:Connect(function(plr)
    local taps = plr.leaderstats.Taps
    taps.Value = taps.Value +1
end)
Ad
Log in to vote
0
Answered by 3 years ago

When answering, if your answer does not fully solve the question, it should be written as a comment to the question instead of as an answer.

Yeah when your getting something from below the line your on the script won't recognize it cuz it hasn't gotten there yet.

Answer this question