once the remote event is catched, it checks if they have enough to buy it
rep.RemoteTools.Sans.Pacfistsans.OnServerEvent:Connect(function(plr)
if weakbitsans == true then
print("you own this character")
elseif plr.leaderstats.Exp.Value == 100 or plr.leaderstats.Exp.Value > 100 then
local data = main:GetAsync(plr.UserId)
data.Exp = data.Exp - 100
sansPacfistmode(plr)
end
end)
im not sure how to use the data store function, does this work?
rep.RemoteTools.Sans.Pacfistsans.OnServerEvent:Connect(function(plr)
local data = mainstore:GetAsync(plr.UserId)
if data.weakbitsans == true then
sansPacfistmode(plr)
elseif plr.leaderstats.Exp.Value == 100 or plr.leaderstats.Exp.Value > 100 then
local data = mainstore:GetAsync(plr.UserId)
plr.leaderstats.Exp = plr.leaderstats.Exp - 100
weakbitsans = true
mainstore:SetAsync(weakbitsans)
sansPacfistmode(plr)
end
end)
i looked in the documents and found some helpfull information, do you think this will work?
rlocal rep = game.ReplicatedStorage
local datastore = game:GetService("DataStoreService")
local mainstore = datastore:GetDataStore("MainDataStuff")
local weakbitsans = false
local savedata = {
["abitweaksans"] = false,
["Lastbreathsans"] = false,
}
rep.RemoteTools.Sans.Weaksans.OnServerEvent:Connect(function(plr)
local leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats then
local data = mainstore:GetAsync(plr.UserId)
if weakbitsans == data then
print("You already own this character")
sansWeakmode(plr)
elseif leaderstats.Exp.Value == 100 or leaderstats.Exp.Value > 100 then
print("Buying Character....")
leaderstats.Exp.Value = leaderstats.Exp.Value - 100
weakbitsans = true
mainstore:SetAsync(plr, weakbitsans)
sansWeakmode(plr)
else
print("Haha your too broke")
end
end
end)
local datastore = game:GetService("DataStoreService")
Then we need the scope by doing
local scope = datastore:GetDatastore("scopeName_1") -- change 1 to 2 and you reset the data for all players etc
There comes the point of setAsynch and getAsynch
Firstly we need to use getAsynch we need a key for this what mostly is the players user id but this can be any string
local data = scope: getAsync ("key_or_plr_userId") -- this the key we also use for setAsync
If data == nil then --we need to create the defould data first when there isn't
Scope:setAsync(key_or_plr_userId, "this can be numbers, bools, tables or strings") -- for tables you only may use strings bools and numbers cuz it needs to match Json)
data = scope: getAsync("key_or_plr_userId") -- set the data to just saved valeu
end
Ok now we have the data and setter up the data store if it won’t already exist
Now we want to save I recommend to do this with an cooldown (to much will fail the data store) or on player leave (rn I’m testing this with an pcall when it fails to save it dents the error massage to discord so I know when it fails but Idk I can fail on leave)
local function Save()
scope:setAsync("key_or_plr_userId", "data to save can be string, tables, numbers or bools") -- tables needs to match Json no things like vector3 or instances
end
rep.RemoteTools.Sans.Weaksans.OnServerEvent:Connect(function(plr)
local leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats then
local data = mainstore:GetAsync(plr.UserId)
if datastore["bitweaksans"] then
print("You already own this character")
sansWeakmode(plr)
elseif leaderstats.Exp.Value == 100 or leaderstats.Exp.Value > 100 then
print("Buying Character....")
leaderstats.Exp.Value = leaderstats.Exp.Value - 100
weakbitsans = true
mainstore:SetAsync(plr, true)
sansWeakmode(plr)
else
print("Haha your too broke")
end
end
end)
uhh heres the full script, did i do anything wrong?
local rep = game.ReplicatedStorage
local datastore = game:GetService("DataStoreService")
local mainstore = datastore:GetDataStore("MainDataStuff")
local weakbitsans = false
local savedata = {
["abitweaksans"] = false,
["Lastbreathsans"] = false,
}
local function sansPacfistmode(plr, model)
local model = game.ReplicatedStorage.CharacterModel:FindFirstChild("Sans")
local oldModel = plr.Character
local newModel = model:Clone()
newModel.Name = plr.Name
plr.Character = newModel
newModel.Parent = game.Workspace
newModel:SetPrimaryPartCFrame(game.workspace.SpawnLocation.CFrame) -- replace Spawn with the respawing part location
for _, object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too.
local newObject = object:Clone()
newObject.Parent = newModel
end
local tools = game.ReplicatedStorage.Tools.SansWeakTools:GetChildren()
for _, tool in ipairs(tools) do
local clonedTool = tool:Clone()
clonedTool.Parent = plr.Backpack
end
oldModel:Destroy()
end
local function LastBreath(plr, model)
local model = game.ReplicatedStorage.CharacterModel:FindFirstChild("LastBreathSans")
local oldModel = plr.Character
local newModel = model:Clone()
newModel.Name = plr.Name
plr.Character = newModel
newModel.Parent = game.Workspace
newModel:SetPrimaryPartCFrame(game.workspace.SpawnLocation.CFrame) -- replace Spawn with the respawing part location
for _, object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too.
local newObject = object:Clone()
newObject.Parent = newModel
end
local tools = game.ReplicatedStorage.Tools.LastBreathTools:GetChildren()
for _, tool in ipairs(tools) do
if tool:IsA("Tool") then
local clonedTool = tool:Clone()
clonedTool.Parent = plr.Backpack
end
end
oldModel:Destroy()
end
local function sansWeakmode(plr, model)
local model = game.ReplicatedStorage.CharacterModel:FindFirstChild("Sans")
local oldModel = plr.Character
local newModel = model:Clone()
newModel.Name = plr.Name
plr.Character = newModel
newModel.Parent = game.Workspace
newModel:SetPrimaryPartCFrame(game.workspace.SpawnLocation.CFrame) -- replace Spawn with the respawing part location
for _, object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too.
local newObject = object:Clone()
newObject.Parent = newModel
end
local tools = game.ReplicatedStorage.Tools.SansBitWeakTools:GetChildren()
for _, tool in ipairs(tools) do
local clonedTool = tool:Clone()
clonedTool.Parent = plr.Backpack
end
oldModel:Destroy()
end
script.Parent.MouseClick:Connect(function(plr)
rep.RemoteTools.Sans.OpenGui:FireClient(plr)
end)
rep.RemoteTools.Sans.Pacfistsans.OnServerEvent:Connect(function(plr)
sansPacfistmode(plr)
end)
rep.RemoteTools.Sans.Weaksans.OnServerEvent:Connect(function(plr)
local leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats then
local data = mainstore:GetAsync(plr.UserId)
if datastore["bitweaksans"] then
print("You already own this character")
sansWeakmode(plr)
elseif leaderstats.Exp.Value == 100 or leaderstats.Exp.Value > 100 then
print("Buying Character....")
leaderstats.Exp.Value = leaderstats.Exp.Value - 100
weakbitsans = true
mainstore:SetAsync(plr, true)
sansWeakmode(plr)
else
print("Haha your too broke")
end
end
end)
rep.RemoteTools.Sans.LastBreath.OnServerEvent:Connect(function(plr)
LastBreath(plr)
end)
rep.RemoteTools.Sans.Weaksans.OnServerEvent:Connect(function(plr)
local leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats then
local success, data = pcall(mainstore.GetAsync, mainstore, plr.UserId)
print("Buying Character....")
if success then
if data then
weakbitsans = data.weakbitsans
end
end
local data = mainstore:GetAsync(plr.UserId)
if weakbitsans then
print("You already own this character")
sansWeakmode(plr)
elseif leaderstats.Exp.Value == 100 or leaderstats.Exp.Value > 100 then
print("BuyingCharacter..")
leaderstats.Exp.Value = leaderstats.Exp.Value - 100
weakbitsans = true
local saveData = {
weakbitsans = if weakbitsans then true else false,
}
mainstore:SetAsync(plr, true)
local success, error = pcall(mainstore.SetAsync, mainstore, plr.UserId, saveData)
if success then return end
sansWeakmode(plr)
else
print("Haha your too broke")
end
end
end)
help, when i add another, it resets the other data, how to add multiple data at once?
local rep = game.ReplicatedStorage
local datastore = game:GetService("DataStoreService")
local mainstore = datastore:GetDataStore("MainDataStuff")
local weakbitsans = false
local lastbreathsans = false
local savedata = {
["weakbitsans"] = false,
["Lastbreathsans"] = false,
["normalsans"] = false,
["genocidesans"] = false,
["vhssans"] = false,
["cotv"] = false,
["tears in the rain"] = false
}
mainstore:SetAsync(savedata)
local function sansPacfistmode(plr, model)
local model = game.ReplicatedStorage.CharacterModel:FindFirstChild("Sans")
local oldModel = plr.Character
local newModel = model:Clone()
newModel.Name = plr.Name
plr.Character = newModel
newModel.Parent = game.Workspace
newModel:SetPrimaryPartCFrame(game.workspace.SpawnLocation.CFrame) -- replace Spawn with the respawing part location
for _, object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too.
local newObject = object:Clone()
newObject.Parent = newModel
end
local tools = game.ReplicatedStorage.Tools.SansWeakTools:GetChildren()
for _, tool in ipairs(tools) do
local clonedTool = tool:Clone()
clonedTool.Parent = plr.Backpack
end
oldModel:Destroy()
end
local function LastBreath(plr, model)
local model = game.ReplicatedStorage.CharacterModel:FindFirstChild("LastBreathSans")
local oldModel = plr.Character
local newModel = model:Clone()
newModel.Name = plr.Name
plr.Character = newModel
newModel.Parent = game.Workspace
newModel:SetPrimaryPartCFrame(game.workspace.SpawnLocation.CFrame) -- replace Spawn with the respawing part location
for _, object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too.
local newObject = object:Clone()
newObject.Parent = newModel
end
local tools = game.ReplicatedStorage.Tools.LastBreathTools:GetChildren()
for _, tool in ipairs(tools) do
if tool:IsA("Tool") then
local clonedTool = tool:Clone()
clonedTool.Parent = plr.Backpack
end
end
oldModel:Destroy()
end
local function sansWeakmode(plr, model)
local model = game.ReplicatedStorage.CharacterModel:FindFirstChild("Sans")
local oldModel = plr.Character
local newModel = model:Clone()
newModel.Name = plr.Name
plr.Character = newModel
newModel.Parent = game.Workspace
newModel:SetPrimaryPartCFrame(game.workspace.SpawnLocation.CFrame) -- replace Spawn with the respawing part location
for _, object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too.
local newObject = object:Clone()
newObject.Parent = newModel
end
local tools = game.ReplicatedStorage.Tools.SansBitWeakTools:GetChildren()
for _, tool in ipairs(tools) do
local clonedTool = tool:Clone()
clonedTool.Parent = plr.Backpack
end
oldModel:Destroy()
end
script.Parent.MouseClick:Connect(function(plr)
rep.RemoteTools.Sans.OpenGui:FireClient(plr)
end)
rep.RemoteTools.Sans.Pacfistsans.OnServerEvent:Connect(function(plr)
sansPacfistmode(plr)
end)
rep.RemoteTools.Sans.Weaksans.OnServerEvent:Connect(function(plr)
local leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats then
local success, data = pcall(mainstore.GetAsync, mainstore, plr.UserId)
print("Buying Character....")
if success then
if data then
weakbitsans = data.weakbitsans
end
end
local data = mainstore:GetAsync(plr.UserId)
if weakbitsans then
print("You already own this character")
sansWeakmode(plr)
elseif leaderstats.Exp.Value == 100 or leaderstats.Exp.Value > 100 then
print("BuyingCharacter..")
sansWeakmode(plr)
leaderstats.Exp.Value = leaderstats.Exp.Value - 100
weakbitsans = true
local saveData = {
lastbreathsans = if lastbreathsans then lastbreathsans else false,
weakbitsans = if weakbitsans then true else false,
}
mainstore:UpdateAsync(plr, true)
local success, error = pcall(mainstore.SetAsync, mainstore, plr.UserId, saveData)
if success then return end
else
print("Haha your too broke")
end
end
end)
rep.RemoteTools.Sans.LastBreath.OnServerEvent:Connect(function(plr)
local leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats then
local success, data = pcall(mainstore.GetAsync, mainstore, plr.UserId)
print("Buying Character....")
if success then
if data then
lastbreathsans = data.lastbreathsans
end
end
local data = mainstore:GetAsync(plr.UserId)
if lastbreathsans then
print("You already own this character")
LastBreath(plr)
elseif leaderstats.Exp.Value == 1200 or leaderstats.Exp.Value > 1200 then
print("BuyingCharacter..")
LastBreath(plr)
leaderstats.Exp.Value = leaderstats.Exp.Value - 1200
lastbreathsans = true
local saveData = {
lastbreathsans = if lastbreathsans then true else false,
weakbitsans = if weakbitsans then weakbitsans else false,
}
mainstore:UpdateAsync(plr, true)
local success, error = pcall(mainstore.SetAsync, mainstore, plr.UserId, saveData)
if success then return end
else
print("Haha your too broke")
end
end
end)
-- load
local data = scope: getAsync (plr.userId) -- this the key we also use for setAsync
If data == nil then --we need to create the defould data first when there isn't
Scope:setAsync(plr.userId, savedate) -- for tables you only may use strings bools and numbers cuz it needs to match Json)
data = scope: getAsync(plr.userId) -- set the data to just saved valeu
end
Make sure that it will be Reset on adding new things you need to handle that but just make sure to make the table and not change it or add handling for new data but I’m busy with that to but it just errors rn I can help you with that when I fixed that
-- save
Scope:setAsync(plr.userId, savedata) -- scope is the datastore variable mainstore