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

Datastore always getting the exact same error?

Asked by 6 years ago
print("game might not work")
gat = "gay"
print("server-sided print...??????????? "..gat)

local DataStore = game:GetService("DataStoreService")
local data = DataStore:GetDataStore("Example")

game.Players.PlayerAdded:connect(function(player)
    local leaderstat = Instance.new("Folder",player)
    leaderstat.Name = "leaderstats"
    local Money = Instance.new("IntValue",leaderstat)
    Money.Name = "money"
    Money.Value = data:GetAsync(player.UserId) or 1
    data:SetAsync(player.UserId, Money.Value)
end)


game.Players.PlayerRemoving:connect(function(player)

    local Money = player.leaderstats.money.Value

        data:SetAsync(player.UserId, Money.Value)
end)

every time i try use it when the player leaves i always get this

ServerScriptService.Script:22: attempt to index local 'Money' (a number value) but i dont have a clue what it means...?

2 answers

Log in to vote
0
Answered by
tantec 305 Moderation Voter
6 years ago

Your script is perfectly fine, it's just at line 22 when you're doing:

data:SetAsync(player.UserId, Money)

It goes completely wrong, because in Lua you add two strings or string and int together with ".." so it would be:

data:SetAsync(player.UserId..Money)

Ad
Log in to vote
0
Answered by 6 years ago

The reason that it is throwing an error is because in line 20 you saved the value of money in the variable Money therefore in line 22 instead of Money.Value just do Money This is a script with the changes made:

local DataStore = game:GetService("DataStoreService")
local data = DataStore:GetDataStore("Example")

game.Players.PlayerAdded:connect(function(player)
    local leaderstat = Instance.new("Folder",player)
    leaderstat.Name = "leaderstats"
    local Money = Instance.new("IntValue",leaderstat)
    Money.Name = "money"
    Money.Value = data:GetAsync(player.UserId) or 1
    data:SetAsync(player.UserId, Money.Value)
end)


game.Players.PlayerRemoving:connect(function(player)

    local Money = player.leaderstats.money.Value

        data:SetAsync(player.UserId, Money)
end)

Answer this question