Ad
Log in to vote
1

i'm having trouble making my own data store it says attempt to index "Stand" how can i fix it?

Asked by 14 days ago
Edited 14 days ago
local datastore = game:GetService("DataStoreService")

local data = datastore:GetDataStore("JOJO")

local players = game:GetService("Players")

local changeevent = game:GetService("ReplicatedStorage").events.changevent

function change(plr, entity, value)

end

players.PlayerAdded:Connect(function(plr)
    local statfolder = Instance.new("Folder")
    statfolder.Name = "StatsFolder"
    statfolder.Parent = plr

    local endurance = Instance.new("IntValue")
    endurance.Name = "Endurance"
    endurance.Parent = statfolder

    local power = Instance.new("IntValue")
    power.Name = "Power"
    power.Parent = statfolder

    local special = Instance.new("IntValue")
    special.Name = "Special"
    special.Parent = statfolder

    local points = Instance.new("IntValue")
    points.Name = "Points"
    points.Parent = statfolder

    local stand = Instance.new("StringValue")
    stand.Name = "Stand"
    stand.Parent = statfolder
    stand.Value = "Standless"

    local exp = Instance.new("IntValue")
    exp.Name = "Experience"
    exp.Parent = statfolder
    exp.Value = "0/100"

    local lvl = Instance.new("NumberValue")
    lvl.Name = "Level"
    lvl.Parent = statfolder
    lvl.Value = 1

    local cash = Instance.new("IntValue")
    cash.Name = "Money"
    cash.Parent = statfolder
    cash.Value = 0

    local maxhealth = Instance.new("NumberValue")
    maxhealth.Name = "HumanoidHealth"
    maxhealth.Parent = statfolder
    maxhealth.Value = 100

    local saves



    local success, errorMessage = pcall(function()
        saves = data:GetAsync(plr.UserId)
    end)
    if not success then
        warn(errorMessage)
    end

    if saves then
        print("Saves Detected")
        stand.Value = saves.Stand
        power.Value = saves.Power
        special.Value = saves.Special
        points.Value = saves.Points
        exp.Value = saves.Experience
        lvl.Value = saves.Level
        cash.Value = saves.Money
        maxhealth.Value = saves.HumanoidHealth
        endurance.Value = saves.Endurance
    else
        warn("No Data Found")
    end
end)

function savegame(plr, StatsFolder)
    local stand = StatsFolder:WaitForChild("Stand")
    local power = StatsFolder:WaitForChild("Power")
    local endurance = StatsFolder:WaitForChild("Endurance")
    local special = StatsFolder:WaitForChild("Special")
    local points = StatsFolder:WaitForChild("Points")
    local maxhealth = StatsFolder:WaitForChild("HumanoidHealth")
    local money = StatsFolder:WaitForChild("Money")
    local lvl = StatsFolder:WaitForChild("Level")

    data:SetAsync(plr.UserId, stand.Value)
    data:SetAsync(plr.UserId, power.Value)
    data:SetAsync(plr.UserId, endurance.Value)
    data:SetAsync(plr.UserId, special.Value)
    data:SetAsync(plr.UserId, points.Value)
    data:SetAsync(plr.UserId, maxhealth.Value)
end

players.PlayerRemoving:Connect(function(plr)
    local PlrStats = plr:WaitForChild("StatsFolder")
    savegame(plr, PlrStats)
    print("Data Saved")
end)
0
What line is the error on? Kingu_Criminal 111 — 13d
0
23:19:47.551 ServerScriptService.DataStoreSystem:72: attempt to index number with 'Stand' - Server - DataStoreSystem:72 Daddy_Francisco -6 — 13d

1 answer

Log in to vote
0
Answered by 13 days ago
Edited 13 days ago

this isn't how DataStores isn't work; it's returning an error instantly for two reasons:

one: you're repeatedly setting one value. for every SetAsync, you're wiping what was originally being stored with the new value you're setting.

two: "Stand" is not an instance of the DataStore. regardless of what the value is set to, you're not going to get an instance out of it. if you want a slightly easier way to do this, I'd suggest putting all of the values into a table and converting it to JSON.

this is a fixed version of the function.

function savegame(plr, StatsFolder)
    local stand = StatsFolder:WaitForChild("Stand")
    local power = StatsFolder:WaitForChild("Power")
    local endurance = StatsFolder:WaitForChild("Endurance")
    local special = StatsFolder:WaitForChild("Special")
    local points = StatsFolder:WaitForChild("Points")
    local maxhealth = StatsFolder:WaitForChild("HumanoidHealth")
    local money = StatsFolder:WaitForChild("Money")
    local lvl = StatsFolder:WaitForChild("Level")

    local st = {
        ["stand"] = stand.Value,
        ["power"] = power.Value,
        ["endurance"] = endurance.Value,
        ["special"] = special.Value,
        ["points"] = points.Value,
        ["maxhealth"] = maxhealth.Value
    }
    data:SetAsync(plr.UserId, game:GetService("HttpService"):JSONEncode(st))
end

and to load the data, you'd do the opposite.

if saves then
    local st = game:GetService("HttpService"):JSONDecode(saves)
    print("Saves Detected")

    stand.Value = st["stand"]
    power.Value = st["power"]
    endurance.Value = st["endurance"]
    special.Value = st["special"]
    points.Value = st["points"]
    maxhealth.Value = st["maxhealth"]
else
    warn("No Data Found")
end

hope this helps

Ad

Answer this question