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

I have a script like this, but it gives an error. How do I fix this?

Asked by 2 years ago

I have a script like this, but it gives an error Error: "stats is not a valid member of Player"

local stats="PlayerStats"
local curent="Tokens"
local reward=100

game.Players.PlayerAdded:Connect(function(plr)
    plr.stats.curent.Value=plr.stats.curent.Value+reward
end)

1 answer

Log in to vote
0
Answered by
Antelear 185
2 years ago
Edited by imKirda 2 years ago

Summary: YOU DO NOT NEED THOSE LOCALS!

When making a local, it's actually called a variable. In this case, you're trying to use variables as a way to go through a directory. Roblox no like that!! Roblox only like you going through directory, not variable!!!!!

So, what do you do? Look at this:

local Users = game:GetService("Players") -- good practise

Users.PlayerAdded:Connect(function(User)
local LStats = Instance.new("Folder")
LStats.Name = "leaderstats"

local Tokens = Instance.new("IntValue")
Tokens.Name = "Tokens"
Tokens.Value = 0
Tokens.Parent = LStats

Tokens.Value+=100

LStats.Parent = User
end)

"But why are we waiting for "leaderstats"? And why do we do everything else?"

When making stuff like this, you'd want to make a sorta leaderboard, to well, keep track of your stats! Not very, hard to understand. Especially if you seen the leaderboard with the simulators!

local LStats = Instance.new("Folder") makes a new folder, which will be where we store our values. Lstats.Name = "leaderstats", we need it to be this name or else it won't work, it's just how it is.

And for instances (LStats and Tokens), it's simply adding a new object via a script (aka could be a script..)

We make our token instance, name it "Tokens", give it the value of 0, etc etc. Also parent it to LStats (leaderstats) or else you won't see any values on the leaderboard (the leaderstats obviously need stats to begin with!)

We use += basically like an INCREMENT! Increment means addition, in the short terms. We're adding 100 to the existing value of 0, but this is simply an example so you can customise it however you'd like! Using += is crucial to save time on scripts, you should get into the habit!

Finally, we set the parent of the leaderstats to the User who joined, with their stats all ready to go, and you will have 100 tokens upon joining as well. This is probably not the most simple way to do it, but it's the best I can do rn with like 0 seconds of sleep aha-

If you wanna learn how to SAVE data such as this, research Datastores. Very crucial in some games.

Click HERE to learn more on how to save your data! <3

I hope this helped you a bit. Variables and directory are very very different things and cannot be combined, unless the variable is a directory itself. I suggest flipping through tutorials that explain variables themselves before trying to understand them better. And if you wanted to, experiment with them yourself!

PRO TIP!

when scripting you may see an orange, red or blue line on some of your code. That means something may be wrong, and you should check out what it is immediately! This is what we call an error, and sometimes they can be invisible, for stuff like that, you'd need to debug (for a later lesson)...

I honestly hope I helped you with this answer, and I also hope you gained some knowledge as well! ^^ (happy new years)

Ad

Answer this question