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

leaderstats is not a valid member of Player?

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago

When you do:

    local leaderstats = Instance.new("Folder")

Replace that line of code with

    local leaderstats = Instance.new("Folder",player)
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. DeceptiveCaster 3761 — 5y
Ad

Answer this question