so im trying to make a tool save but the tool just wont save!
code:
example:- YouTube
yeah i put one by set async and when the tool loads and neither of them show up.
i also get a refund when i buy the tool and leave and rejoin.
I usually use pcall to set and get async. Then if there are any errors loading or saving, I can print the error , and if it succeeded, I can load the player data (load the saved data) or save the current data. I’ll edit an example:
local success, err = pcall(function()
DataStore:SetAsync("User_"..player.UserId, toolsave)
end)
if(success) then
print("Tool's saved successfully!")
else
print("There was an error saving the tools.")
end
One more question, how do you give the player the tool? Is it from a local script? Because if it is, you can’t save it. The server “doesn’t know” that the player got the tool, and that might explain why you are getting a refund, because you are also changing the cash from a local script, and it does not replicate to the server.
yeah i give it through a local script
how do i change that?
30char
You should fire a remote event from the client (local script) to the server (script in the ServerScriptService.), and give the player the tool and decrease the cash from there. for example:
Create a remote event in the ReplicatedStorage
for example:
local script:
script.Parent.MouseButton1Click:Connect(function()
game:GetService("ReplicatedStorage").RemoteEvent:FireServer("Gun1", 500) --Fire the tool name and cash amount for example.
end)
And the server script:
game:GetService("ReplicatedStorage").RemoteEvent.OnServerEvent:Connect(function(player, toolName, price)
if(player.leaderstats.Cash.Value >= price) then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - price
game.ServerStorage:WaitForChild(toolName):Clone().Parent = player.Backpack.
end
end)
You can only save data from the server, and client changes don’t replicate to the server. So the server can’t save them, because it doesn’t know that they even exist.
And I still think you should pcall the GetAsync() and SetAsync() to handle failures(failures can happen in datasores)
Like this to save:
local success, err = pcall(function()
DataStore:SetAsync("User_"..player.UserId, toolsave)
end)
if(success) then
print("Tool's saved successfully!")
else
print("There was an error saving the tools.")
warn(err)
end
And this to load:
local tool
local success, err = pcall(function()
tool = DataStore:GetAsync("User_"..player.UserId)
end)
if(success) then
print("Tool's loaded successfully!")
--Load your tools here
else
print("There was an error saving the tools.")
warn(err)
end
i just tried that and it still dosent work
What did you try? The Pcalls? or the remtoe events?
i tried the remote events
30charrs
Did you just copy my script? You need to change it to what fits your shop. Send the output here if you have any.
it saves the data but dosent load it.
Does it print that it saved? 30charrrsss
yeah it prints that it saved but dosent print that it loads
Can you please send here the data store script with the pcalls?
You should use Pcalls, in case the data fails to load or save. And it will print it anyways because it’s the next line in the code.
Look at my previous post.
Oh wait I am sorry, you called the whole thing at a pcall, that’s not what I meant, let me explain again:
You shouldn’t call the whole thing as a pcall. Just the part of the GetAsync and SetAsync should be in a pcall. Also, save the pcall function in a variable. like this:
local success, err = pcall(function()
DataStore:SetAsync("User_"..player.UserId, toolsave)
end)
And like this:
local tool
local success, err = pcall(function()
tool = DataStore:GetAsync("User_"..player.UserId)
end)
if(success) then
print("Tool's loaded successfully!")
--Load your tools here
else
print("There was an error saving the tools.")
warn(err)
end
And only in the if success then, you should load your data, because how do you expect the server to load the data of the player if it fails?
My draft didn’t save so I edit again:. But my point here is that data store requests can fail. And that’s why we use the pcall function. When the pcall function makes the success true, we know that it managed to Get/SetAsync() and we can load/save the data. But what you did here, is just like calling a normal function. You didn’t even get the errors/success from the pcall. @EthanRobloxYT123 I hope that helps and I am positive that it will work for you