Attempt to index number with 'LevelStats'

So i keep getting this error that says ServerScriptService.Script:7: attempt to index number with 'LevelStats', and the error is coming from the exp variable at line 7.
local exp = player:WaitForChild("LevelStats"):WaitForChild("Current")
i tried to use this as well local exp = player.LevelStats:WaitForChild("Current") but it gave me a different error that said local exp = player.LevelStats:WaitForChild("Current"). The script is in serverscriptservice, and i don’t know if its something dealing with that.
Here is the script.

local ResourceSystem = game.Workspace.ResourceSystem
local respawnTime = ResourceSystem.Rocks.RespawnTime

for i, Rock in pairs(ResourceSystem.Rocks:GetChildren()) do
	if Rock.Name == "Normal Rock" then
		Rock.Health.Changed:Connect(function(player)
			local exp = player:WaitForChild("LevelStats"):WaitForChild("Level")
			if Rock.Health.Value <= 0 then
				print("Rock Mined!")
				local random = math.random(5,15)
				exp.Value = exp.Value + Random
				
				spawn(function()
					Rock.Parent = game:GetService("ServerStorage").Resources:WaitForChild(Rock.Name)
					Rock.Health.Value = 100
					wait(respawnTime.Value)
					Rock.Parent = workspace.ResourceSystem.Rocks
				end)
			end
		end)
	end
end

Any help would be appreciated!

The Changed Event returns back the New Value, not the Player Instance

It depends on where you exactly get the Player from, as you should first reference it by your variables instead of inside the loop

If this a LocalScript, you can easily define the Player by doing game.Players.LocalPlayer

Doing it from a Server Script however, you’ll have to find some other way to get the Player

2 Likes

So I assume all of your rock items have humanoids for the health? Well right here you try to get a “player” parameter, which does not exist. The only parameter that is returned by this function is the new health of the humanoid after being changed.

So the “player” value is a number, and you’re trying to look for “LevelStats” (which I assume is a folder) inside a number, which is impossible.

In order to get the player, you’ll need to add some sort of tag system to see who’s hitting the rocks.

1 Like

imma see if I can use a PlayerAdded event to get the player

The rocks have an int value set to “100” instead of humanoids, the int is their health/hp. I get it now with the player variable too, I’m try using a playeradded event at the beginning of the script.

Yeah, so if the health is an int value.Changed will work. Adding a tag system is quite easy you can take a look at the default roblox sword for an example of that. Then just use a single script with a for loop in your rock folder to detect when one has been completely mined and give the player whatever you want to.

1 Like

The PlayerAdded event fixed it! Here’s the current script if you like to see it

local ResourceSystem = game.Workspace.ResourceSystem
local respawnTime = ResourceSystem.Rocks.RespawnTime

game.Players.PlayerAdded:Connect(function(player)
	local exp = player:WaitForChild("LevelStats"):WaitForChild("Current")
	for i, Rock in pairs(ResourceSystem.Rocks:GetChildren()) do
		if Rock.Name == "Normal Rock" then
			Rock.Health.Changed:Connect(function()
				
				if Rock.Health.Value <= 0 then
					print("Rock Mined!")
					local randomExp= math.random(5,15)
					exp.Value = exp.Value + randomExp

					spawn(function()
						Rock.Parent = game:GetService("ServerStorage").Resources:WaitForChild(Rock.Name)
						Rock.Health.Value = 100
						wait(respawnTime.Value)
						Rock.Parent = workspace.ResourceSystem.Rocks
					end)
				end
			end)
		end
	end
end)