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

Why does it say that leaderstats is not a valid member? I used waitforchild too.

Asked by 3 years ago

Below is the script that's erroring.

local Players = game:GetService("Players")

local kickCommand = "!kick "

local function onOwnerChatted(player, message)
    if message:sub(1, kickCommand:len()):lower() == kickCommand:lower() then
        local name = message:sub(kickCommand:len() + 1)
        local playerToKick = Players:FindFirstChild(name)
        if playerToKick then
            playerToKick:Kick("Our moderators have determined that your behavior at SF Nuclear Lab has been in violation of our rules. Therefore, you are kicked from this game.")
        else

        end
    end
end

local function onPlayerAdded(player)
    if player.leaderstats.GVC2YDJ5.Value >= 1 then
        player.Chatted:Connect(function (...)
            onOwnerChatted(player, ...)
        end)
    end
end

for _, player in pairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

Below is the leaderstats script.


local Players = game:GetService("Players") local DataStoreService = game:GetService("DataStoreService") local TestDataStore = DataStoreService:GetDataStore("emp43") local data = { ["22XQ3SGH"] = 0, ["XTSAT8B7"] = 0, ["RVESMDXG"] = 0, ["8YHCNKMD"] = 0, ["D4VWKQF7"] = 0, ["ENB78YBQ"] = 0, ["GVC2YDJ5"] = 0, ["TKBRVCBQ"] = 0, ["6TU53T5F"] = 0, ["A79QBG5C"] = 0 } Players.PlayerAdded:Connect(function(Player) local DataFromStore = nil local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = Player for name, value in pairs(data) do local new = Instance.new("NumberValue") new.Name = name new.Value = value new.Parent = leaderstats end local s, e = pcall(function() DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId) end) if s then print("Getting Data For: " .. Player.Name) if DataFromStore then for name, value in pairs(DataFromStore) do Player.leaderstats[name].Value = value end print("Data Loaded For: " .. Player.Name) else print("Created New Data For: " .. Player.Name) end else warn("Error Getting Data For: " .. Player.Name) end -- Check if banned. --if Player:WaitForChild("leaderstats").ENB78YBQ.Value >= 1 then --Player:Kick("Our moderators have determined that your behavior at SF Nuclear Lab has been in violation of our rules. Therefore, you are banned from this game.") --end end) Players.PlayerRemoving:Connect(function(Player) local DataForStore = {} for name, value in pairs(Player.leaderstats:GetChildren()) do DataForStore[value.Name] = value.Value end local success = pcall(TestDataStore.SetAsync, TestDataStore, "uid-" .. Player.UserId, DataForStore) if success then print("Successfully Saved Data For: " .. Player.Name) end end) game:BindToClose(function() for _, v in pairs(game.Players:GetPlayers()) do local success, errormessage = pcall(function() TestDataStore:SetAsync(v.UserId, data) -- define the data, since it's in a different scope end) end end)

1 answer

Log in to vote
0
Answered by 3 years ago

First off it should be a serversided script within server script service. You should remove the waitforchild considering they are not very good in serversided scripts. Try this.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("emp43")

local data = {
    ["22XQ3SGH"] = 0,
    ["XTSAT8B7"] = 0,
    ["RVESMDXG"] = 0,
    ["8YHCNKMD"] = 0,
    ["D4VWKQF7"] = 0,
    ["ENB78YBQ"] = 0,
    ["GVC2YDJ5"] = 0,
    ["TKBRVCBQ"] = 0,
    ["6TU53T5F"] = 0,
    ["A79QBG5C"] = 0
}

Players.PlayerAdded:Connect(function(Player)
    local DataFromStore = nil

    local leaderstats = Instance.new("Folder", Player) --Parent it within the instance here not there.
    leaderstats.Name = "leaderstats"

    for name, value in pairs(data) do
        local new = Instance.new("NumberValue", leaderstats) --Same thing here.
        new.Name = name
        new.Value = value
    end

    local s, e = pcall(function()
        DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
    end)

    if s then
        print("Getting Data For: " .. Player.Name)
        if DataFromStore then
            for name, value in pairs(DataFromStore) do
                Player.leaderstats[name].Value = value
            end
            print("Data Loaded For: " .. Player.Name)
        else
            print("Created New Data For: " .. Player.Name)
        end
    else 
        warn("Error Getting Data For: " .. Player.Name)
    end

    -- Check if banned.

   if Player.leaderstats.ENB78YBQ.Value >= 1 then --Assuming your having the problem here. I will fix it up.
        Player:Kick("Our moderators have determined that your behavior at SF Nuclear Lab has been in violation of our rules. Therefore, you are banned from this game.")
   end
end)

Players.PlayerRemoving:Connect(function(Player)
    local DataForStore = {}

    for name, value in pairs(Player.leaderstats:GetChildren()) do
        DataForStore[value.Name] = value.Value
    end

    local success = pcall(TestDataStore.SetAsync, TestDataStore, "uid-" .. Player.UserId, DataForStore)

    if success then
        print("Successfully Saved Data For: " .. Player.Name)
    end
end)

game:BindToClose(function()
    for _, v in pairs(game.Players:GetPlayers()) do
        local success, errormessage = pcall(function()
            TestDataStore:SetAsync(v.UserId, data) -- define the data, since it's in a different scope
        end) 
    end
end)

0
Ok, I will try! A_DistractionToYou 4 — 3y
0
That didn't fix it. The problem isn't the leaderstat script. The problem is in the chatcommand script where this error appears. A_DistractionToYou 4 — 3y
0
  15:20:05.358 leaderstats is not a valid member of Player "Players.A_DistractionToYou" - Server - ChatCommands:18 A_DistractionToYou 4 — 3y
0
Publish your game go to game settings then enable studio access to api. Then start a test go to players in explorer then into yourself then leaderstats and make sure everything is as it should be. verysweatyperson 10 — 3y
0
The leaderstat script works, but the script that is erroring isn't. Do you want to talk about this over discord? A_DistractionToYou 4 — 3y
Ad

Answer this question