--//Datastore
local DataStoreService = game:GetService("DataStoreService") --Gets the DataStoreService
local dataStore = DataStoreService:GetDataStore("StatsD") --Gives the Datastore a name
--//Function
local function saveData(player)
local tableToSave = {
player.leaderstats.Strenght.Value;
player.leaderstats.Defense.Value;
player.leaderstats.KI.Value;
player.leaderstats.Physic.Value;
player.leaderstats.TP.Value;
}
local success, errorMessage = pcall(dataStore.SetAsync, dataStore, player.UserId, tableToSave)
if success then
print("Data has been saved!")
else --If datastore fails then
print("Data has not been saved!")
end
end
--//leaderstats
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Wait()
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Strenght= Instance.new("IntValue")
Strenght.Name = "Strenght"
Strenght.Parent = leaderstats
Strenght.Value = 0
local Defense= Instance.new("IntValue")
Defense.Name = "Defense"
Defense.Parent = leaderstats
Defense.Value = 0
local KI= Instance.new("IntValue")
KI.Name = "KI"
KI.Parent = leaderstats
KI.Value = 0
local Psyhic= Instance.new("IntValue")
Psyhic.Name = "Physic"
Psyhic.Parent = leaderstats
Psyhic.Value = 0
local TP= Instance.new("IntValue")
TP.Name = "TP"
TP.Parent = leaderstats
TP.Value = 0
local data = nil
local success, errorMessage = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
Strenght.Value = data[1]
Defense.Value = data[2]
KI.Value = data[3]
Psyhic.Value = data[4]
TP.Value = data[5]
else
print("The Player has no Data!")
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
game:BindToClose(function()
for _, player in ipairs(game.Players:GetPlayers()) do
task.spawn(saveData, player)
end
end)
i was learning scripting off yt tutorials i watched video typed the code got into game and it didnt save data i dont know but it wont show any errors
You are setting the first parameter of the setasync to the datastore, when it should be the key. If you remove the dataStore, bit from it, does it function as intended?
You’re code to save the desired table is for the moment a non sens since you need to write it like this I believe :
local success = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then
print("Data has been saved!")
else --If datastore fails then
warn("Data has not been saved!")
end
Yes, if you want to save your leaderstats values you need to change them from the server cause when you change the value locally it will change nothing for the server !
this is the script it wont give any errors but when i click the keybind it wont give
game.Players.PlayerAdded:Connect(function(plr)
local uis = game:GetService("UserInputService")
plr:WaitForChild("leaderstats")
local _stats = plr.leaderstats
local strenght = _stats.Strenght
local Defense = _stats.Defense
local KI = _stats.KI
local Physic = _stats.Physic
local smulti = _stats:WaitForChild("smulti")
local kmulti = _stats:WaitForChild("kmulti")
local dmulti = _stats:WaitForChild("dmulti")
local pmulti = _stats:WaitForChild("pmulti")
local tp = _stats.TP
local add = game:GetService("ReplicatedStorage").Add.Value
local db = false
uis.InputBegan:Connect(function(input, gameProccesedEvent)
if input.KeyCode == Enum.KeyCode.F then
if db == false then
db = true
strenght.Value=strenght.Value + add.Value * smulti.Value
wait(0.2)
db = false
end
end
if input.KeyCode == Enum.KeyCode.Q then
if db == false then
db = true
KI.Value=KI.Value + add.Value * kmulti.Value
wait(0.2)
db = false
end
end
if input.KeyCode == Enum.KeyCode.V then
if db == false then
db = true
Defense.Value=Defense.Value + add.Value * dmulti.Value
wait(0.2)
db = false
end
end
if input.KeyCode == Enum.KeyCode.Y then
if db == false then
db = true
Physic.Value=Physic.Value + add.Value * pmulti.Value
wait(0.2)
db = false
end
end
end)
while wait(0.2) do
tp.Value = strenght.Value + Physic.Value + KI.Value + Defense.Value
end
end)
You can’t acces UserInputService since your script is server sided !
You’ll have to send a remote event each time a value change to the server !
If I was you I would make a dictionary named PlayersData and put into it at the beginning each players data with for key their userid and so update it while the game is running and when someone leave get his data and destroy them !
--//Datastore
local DataStoreService = game:GetService("DataStoreService") --Gets the DataStoreService
local dataStore = DataStoreService:GetDataStore("Stast") --Gives the Datastore a name
--//Function
local function saveData(player)
local tableToSave = {
player.leaderstats.Strenght.Value;
player.leaderstats.Defense.Value;
player.leaderstats.KI.Value;
player.leaderstats.Physic.Value;
player.leaderstats.TP.Value;
}
local success = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then
print("Data has been saved!")
else --If datastore fails then
warn("Data has not been saved!")
end
end
--//leaderstats
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Wait()
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Strenght= Instance.new("IntValue")
Strenght.Name = "Strenght"
Strenght.Parent = leaderstats
Strenght.Value = 0
local Defense= Instance.new("IntValue")
Defense.Name = "Defense"
Defense.Parent = leaderstats
Defense.Value = 0
local KI= Instance.new("IntValue")
KI.Name = "KI"
KI.Parent = leaderstats
KI.Value = 0
local Physic= Instance.new("IntValue")
Physic.Name = "Physic"
Physic.Parent = leaderstats
Physic.Value = 0
local TP= Instance.new("IntValue")
TP.Name = "TP"
TP.Parent = leaderstats
TP.Value = 0
local smulti= Instance.new("IntValue") --Creates an IntValue
smulti.Name = "smulti" --Sets IntValue name to "deaths" (If you change this make sure you change all of them)
smulti.Parent = leaderstats --Puts the IntValue under the "leaderstats" folder
smulti.Value = 1 --Gives the IntValue Value to start off with
local dmulti= Instance.new("IntValue") --Creates an IntValue
dmulti.Name = "dmulti" --Sets IntValue name to "deaths" (If you change this make sure you change all of them)
dmulti.Parent = leaderstats --Puts the IntValue under the "leaderstats" folder
dmulti.Value = 1 --Gives the IntValue Value to start off with
local pmulti= Instance.new("IntValue") --Creates an IntValue
pmulti.Name = "pmulti" --Sets IntValue name to "deaths" (If you change this make sure you change all of them)
pmulti.Parent = leaderstats --Puts the IntValue under the "leaderstats" folder
pmulti.Value = 1 --Gives the IntValue Value to start off with
local kmulti= Instance.new("IntValue") --Creates an IntValue
kmulti.Name = "kmulti" --Sets IntValue name to "deaths" (If you change this make sure you change all of them)
kmulti.Parent = leaderstats --Puts the IntValue under the "leaderstats" folder
kmulti.Value = 1 --Gives the IntValue Value to start off with
local uis = game:GetService("UserInputService")
local add = game:GetService("ReplicatedStorage").Add.Value
local db = false
uis.InputBegan:Connect(function(input, gameProccesedEvent)
if input.KeyCode == Enum.KeyCode.F then
if db == false then
db = true
Strenght.Value=Strenght.Value + add.Value * smulti.Value
wait(0.2)
db = false
end
end
if input.KeyCode == Enum.KeyCode.Q then
if db == false then
db = true
KI.Value=KI.Value + add.Value * kmulti.Value
wait(0.2)
db = false
end
end
if input.KeyCode == Enum.KeyCode.V then
if db == false then
db = true
Defense.Value=Defense.Value + add.Value * dmulti.Value
wait(0.2)
db = false
end
end
if input.KeyCode == Enum.KeyCode.Y then
if db == false then
db = true
Physic.Value=Physic.Value + add.Value * pmulti.Value
wait(0.2)
db = false
end
end
end)
while wait(0.2) do
TP.Value = Strenght.Value + Physic.Value + KI.Value + Defense.Value
end
local data = nil
local success, errorMessage = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
Strenght.Value = data[1]
Defense.Value = data[2]
KI.Value = data[3]
Physic.Value = data[4]
TP.Value = data[5]
else
print("The Player has no Data!")
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
game:BindToClose(function()
for _, player in ipairs(game.Players:GetPlayers()) do
task.spawn(saveData, player)
end
end)
remote events server script
local r = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(plr)
plr:WaitForChild("leaderstats")
local s = plr.leaderstats.Strenght
local d = plr.leaderstats.Defense
local k = plr.leaderstats.KI
local p = plr.leaderstats.Physic
local t = plr.leaderstats.TP
local smulti = plr.leaderstats.smulti
local dmulti = plr.leaderstats.dmulti
local kmulti = plr.leaderstats.kmulti
local pmulti = plr.leaderstats.pmulti
local add = r.Add.Value.Value
r.s.OnServerEvent:Connect(function()
s.Value = s.Value + add * smulti.Value
print("strenght ran!")
end)
r.d.OnServerEvent:Connect(function()
d.Value = d.Value + add * dmulti.Value
print("defense ran!")
end)
r.k.OnServerEvent:Connect(function()
print("KI ran!")
k.Value = k.Value + add * kmulti.Value
end)
r.p.OnServerEvent:Connect(function()
print("Physic ran!")
p.Value = p.Value + add * pmulti.Value
end)
end)
remote local script
local uis = game:GetService("UserInputService")
local r = game:GetService("ReplicatedStorage")
uis.InputBegan:Connect(function(input,gameProccesedEvent)
if input.KeyCode == Enum.KeyCode.F then
r.s:FireServer()
end
end)
uis.InputBegan:Connect(function(input,gameProccesedEvent)
if input.KeyCode == Enum.KeyCode.V then
r.d:FireServer()
end
end)
uis.InputBegan:Connect(function(input,gameProccesedEvent)
if input.KeyCode == Enum.KeyCode.Q then
r.k:FireServer()
end
end)
uis.InputBegan:Connect(function(input,gameProccesedEvent)
if input.KeyCode == Enum.KeyCode.Y then
r.p:FireServer()
end
end)
No read my code again please I will make you an exemple :
local r = game:GetService("ReplicatedStorage")
r.s.OnServerEvent:Connect(function(plr)
local add = r.Add.Value.Value
local smulti = plr.leaderstats.smulti
local s = plr.leaderstats.Strenght
s.Value = s.Value + add * smulti.Value
print("strenght ran!")
end)