Invalid argument #2 (string expected, got Instance)

I am currently getting this error in my output:

invalid argument #2 (string expected, got Instance)

Here is my code:

local points = game.Players[game.Players.LocalPlayer].leaderstats.Points.Value

script.Parent.Points.Frame.TextLabel.Text = "Points: " .. points

I am guessing I am experiencing this issue because leaderstats.Points.Value is not a string. However, I don’t know how to convert it into a string.

Any sort of response would be appreciated! Thanks!

2 Likes

The issue isn’t concatenation its game.Players[game.Players.LocalPlayer]. If there isn’t a particular reason why you’d need to index game.Players with the player instance then i would just do:

local points = game.Players.LocalPlayer.leaderstats.Points.Value
1 Like

Use the tostring() function on the value.

local points = tostring(game.Players.LocalPlayer.leaderstats.Points.Value)
4 Likes

Your error is because you are trying to reference the player instance in game.Players.

To properly reference the player all you need to do is the same thing but without game.Players[].

local player = game:GetService("Players").LocalPlayer -- Reference the player
local points = player.leaderstats.Points.Value -- Reference the points leaderstat in the player instance

script.Parent.Points.Frame.TextLabel.Text = "Points: " .. points

Also as @juliaoverflow stated if you ever need to convert something into a string you can just use the tostring() function (but its not needed in this scenario).

Anyway, hope this helps and goodluck!

1 Like

Now I have this issue: attempt to index number with 'Changed'

Code:

local points = game.Players.LocalPlayer.leaderstats.Points.Value

script.Parent.Points.Frame.TextLabel.Text = "Points: " .. points

points.Changed:Connect(function(NewValue)
   script.Parent.Points.Frame.TextLabel.Text = "Points: " .. points
end)

You need to index the instance itself. Use Ze_tsu’s suggestion to use GetService and try this:

local Players = game:GetService("Players")
local points = Players.LocalPlayer.leaderstats.Points

script.Parent.Points.Frame.TextLabel.Text = "Points: " .. points.Value

points.Changed:Connect(function(NewValue)
   script.Parent.Points.Frame.TextLabel.Text = "Points: " .. points.Value
end)
4 Likes

Thanks so much! You’ve helped me out a lot!