Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

leaderstats is not a valid member of Player?

Yesterday i was working on a new game (my first one as you could probably tell) but the code and everything worked fine yesterday and didnt change anything after that anymore, now that i want to continue working today i try running the game to make sure everything is still fine and i get met with the message: leaderstats is not a valid member of Player and i dont know whats wrong, could someone help? heh. again. Thanks!

local DataStore = game:GetService("DataStoreService")
local PointsData = DataStore:GetOrderedDataStore("Points")

game.Players.PlayerAdded:Connect(function(player)
	local points = PointsData:GetAsync(player.UserId)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local pointsValue = Instance.new("IntValue",leaderstats)
	pointsValue.Value = points
	leaderstats.Parent = player
	pointsValue.Name = "Strength"
end)

game.Players.PlayerRemoving:Connect(function(player)
	local strength = player.leaderstats.Strength.Value
	PointsData:UpdateAsync(player.UserId, strength)
    print("Saved Data!")
end)

game.ReplicatedStorage.BuyItem.OnServerEvent:Connect(function(player,weight)
	if player.leaderstats.Strength.Value >= weight.Price.Value then
		player.leaderstats.Strength.Value = player.leaderstats.Strength.Value - weight.Price.Value
		local newWeight = weight:Clone()
		local oldWeight = player.Backpack:FindFirstChildOfClass("Tool")or player.Character:FindFirstChildOfClass("Tool")
		oldWeight:Destroy()
		newWeight.Parent = player.Backpack
	end
end)

1 answer

✓ Accepted answer

When you do:

    local leaderstats = Instance.new("Folder")

Replace that line of code with

    local leaderstats = Instance.new("Folder",player)

1 comment

0

It is highly recommended that you do not use the second parameter of Instance.new(). You should set the Parent after you have set anything else you want to set, such as the Name of the Instance.

This discussion is preserved as it was. New answers, comments, and votes are closed.