Can you find a leaderstats using a username in a StringValue?

Hello, I have this Script and it is in a Tool. The Tool has a StringValue named Crafter and the Script is supposed to Read that StringValue’s Value which is a Username, and it is supposed to Check that StringValue’s Value and Find the Player by the Username and find the Leaderstats.

I do not get an Error, but it doesn’t work.
By the way I do get a Infinite Yield Warning.
Infinite yield possible on ‘Players:WaitForChild(“”)’ - Studio

Here is the Code:

local tool = script.Parent
local ownerCrafter = tool.Crafter

local addEvent = tool:FindFirstChild("addXP")

-- get the players username from the ownerCrafter stringValue's value which is their username
local player = game:GetService("Players"):WaitForChild(ownerCrafter.Value)
print(player.Name)

local leaderstats = player:FindFirstChild("leaderstats")
local XP = leaderstats and leaderstats:FindFirstChild("XP")

local function addXP()
	if XP then
		XP.Value = XP.Value + 1
		print("value added")
	end
end

addEvent.OnServerEvent:Connect(addXP)
1 Like

Sidenote: please stop using an excessive amount of bold text in your formatting.
Are you testing this immediately after startup? You should try waiting a bit for it to load if this is on the client.

2 Likes

This is a Script in a Tool.

1 Like

I also recommend that you stop using an excessive amount of bold text.

But, uh, did you try waiting a bit as was suggested?

1 Like

Whatever, I’ll use bold still. But yes I added a wait and now it works.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.