i have a database that works fine, but there is a problem in line 11-12, i am using a remote event to fetch intval in playergui, it is a local remote event, here is the code for the database script and remote event script:
--database
local remote = game.ReplicatedStorage.Shop
local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("Info")
local function saveData(player) -- The functions that saves data
local tableToSave = {
player.leaderstats.Gold.Value; -- First value from the table
player.leaderstats.WalkSpeed.Value; -- Second value from the table
remote.FetchPriceAndMax:FireClient(player)[1];
remote.FetchPriceAndMax:FireClient(player)[2]
}
local success, err = pcall(function()
Data:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
end)
if success then -- If the data has been saved
print("Data has been saved!")
else -- Else if the save failed
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(p)
-- Currency
local Leaderstats = Instance.new("Folder", p)
Leaderstats.Name = "leaderstats"
local gold = Instance.new("IntValue", Leaderstats)
local walkspeed = Instance.new("IntValue", Leaderstats)
gold.Name = "Gold"
walkspeed.Name = "WalkSpeed"
-- DataBase
local data
local success, err = pcall(function()
data = Data:GetAsync(p.UserId)
end)
if success then
gold.Value = data[1]
walkspeed.Value = data[2]
remote.ChangePriceAndMax:FireClient(p, data[3], data[4])
else
print("Couldn't load!")
warn(err)
end
p.CharacterAdded:Connect(function()
p.Character.Humanoid.WalkSpeed = walkspeed.Value+16
p.Character.Humanoid.Died:Connect(function()
p.Team = game.Teams.spectator
end)
walkspeed.Changed:Connect(function()
print("Changed!!")
p.Character.Humanoid.WalkSpeed = walkspeed.Value+16
end)
end)
game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
warn(err)
end
end)
game:BindToClose(function() -- When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
warn(err)
end
end
end)
end)
--event
local remote = game.ReplicatedStorage.Shop.FetchPriceAndMax
remote.OnClientEvent:Connect(function(p)
local pl = p.PlayerGui.ScreenGui.shop.Speed
return {pl.max.Value, pl.speedprice.Value}
end)