Adding Leaderstats

So I am making a mod panel and one of the options I am making is a refund system so you can type the player’s name in and how much you want to add to their leaderstats. This is my script:

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

local Players = game:GetService("Players")
     
local cache = {}

function getUserIdFromUsername(name)
	if cache[name] then return cache[name] end
	local player = Players:FindFirstChild(name)
	if player then
		cache[name] = player.UserId
		return player.UserId
	end 
	
	local id
	
	pcall(function ()
		id = Players:GetUserIdFromNameAsync(name)
	end)
	
	cache[name] = id
	return id
end

local Amount = script.Parent.Parent.Parent.Amount.TextBox.Text --fixed this line
local Player = script.Parent.Parent.Parent.Player.TextBox.Text --fixed this line
local PlayerId = getUserIdFromUsername(Player)

script.Parent.MouseButton1Click:connect(function()
	DataStore:SetAsync(PlayerId, {
        --["Points"] = Player.leaderstats.Points.Value + 1;
        ["Wins"] = Player.leaderstats.Wins.Value + Amount;
	    })
end)

This is the error that I am getting:

leaderstats is not a valid member of TextBox

It is erroring on this line:

["Wins"] = Player.leaderstats.Wins.Value + Amount;

I would appreciate it if someone could please help me out, thanks.

1 Like

It might be because you are saying this:

local Player = script.Parent.Parent.Parent.Player.TextBox

you are assigning “Player” to the textbox, you forgot the .Text at then end and the script thinks it’s a textbox

1 Like

Thanks, that was a silly mistake, lol. Now I am getting this error:

Players.Aka_Ethan110.PlayerGui.Menu.ModPanel.Refund.TextButton.Script:33: attempt to index nil with ‘Wins’

It is erroring on this line:

["Wins"] = Player.leaderstats.Wins.Value + Amount;

I don’t think it’s required to use a table but it should just be, I also don’t know if you should add it in the function or before the function

-- Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value + amount
SetAsync(PlayerId,{Player.leaderstats.Wins.Value})

also don’t forget the player mistake

It still errors:

Players.Aka_Ethan110.PlayerGui.Menu.ModPanel.Refund.TextButton.Script:31: attempt to index nil with 'Wins'

I think the leaderstats are nil, you can check if they exist using an if statement:

if Player:FindFirstChild("leaderstats") then
--Your code
end

(or the player is nil, to check this, you can print it out)

print(Player)
print(Player:FindFirstChild("leaderstats")

My leaderstats aren’t nil as seen here:

When I run this code:

print(Player)
print(Player:FindFirstChild("leaderstats"))

I get this in the output:

Username
Players.Aka_Ethan110.PlayerGui.Menu.ModPanel.Refund.TextButton.Script:31: attempt to call a nil value

um, is that a normal script, I don’t think normal scripts should be used for ui

Make player game.Players[script.Parent.Parent.Parent.Player.TextBox.Text]

It’s a normal script because I can’t access DataStores through LocalScripts.

I mean you could use remote events if you know how to lol

You use localscripts for ui. Use RemoteEvents to handle stuff like datastores.

Edit: Fixed link.

I’m a beginner, I don’t really know how to use them. ;-;

you can use youtube or check this out:

Remote Events

Documentary:
Remote Functions and Events