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

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

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

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)

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

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