hello im trying to make datastore2 save characters like Defend The Train but when i do that it doesnt actually save and just set it to Hat
this is the code
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local Workspace = game:GetService("Workspace")
local Hats = game:GetService("ReplicatedFirst"):WaitForChild("Hats")
local DataStore2 = require(ServerScriptService.DataStore2)
-- Combine every key you use. This will eventually be the default, but for now read the "Gotchas" section to understand why we need this.
DataStore2.Combine("loldata", "points")
DataStore2.Combine("loldata","char")
Players.PlayerAdded:Connect(function(player)
local DefaultCharacter = {
Hats.Hat, --hat 1
nil, -- hat 2
nil, -- hat 3
nil, -- torso color
nil -- shirt graphic
}
local pointsStore = DataStore2("points", player)
local characterstore = DataStore2("char",player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local characterdata = characterstore:Get(DefaultCharacter)
local Char = player.Character
print('Loading character...')
for i,v in ipairs(characterdata) do
print(i)
print(v)
if v ~= nil then
if v:IsA('Accessory') then
v:Clone().Parent = Char
if i == 4 then
Char:WaitForChild("Torso").Color = v
elseif i == 5 then
Char:WaitForChild("Shirt Graphic").Graphic = v
end
end
end
end
local points = Instance.new("NumberValue")
points.Name = "Points"
points.Value = pointsStore:Get(0) -- The "0" means that by default, they'll have 0 points
points.Parent = leaderstats
pointsStore:OnUpdate(function(newPoints)
-- This function runs every time the value inside the data store changes.
points.Value = newPoints
end)
characterstore:OnUpdate(function(newChar)
if newChar ~= characterdata then
characterdata = newChar
for i,v in ipairs(Char:GetChildren()) do
if v:IsA("Accessory") then
v:Destroy()
end
end
print('Loading character...')
--characterstore:Set(characterdata)
for i,v in ipairs(characterdata) do
print(i)
print(v)
if v ~= nil then
if v:IsA('Accessory') then
v:Clone().Parent = Char
if i == 4 then
Char:WaitForChild("Torso").Color = v
elseif i == 5 then
Char:WaitForChild("Shirt Graphic").Graphic = v
end
end
end
end
end
end)
leaderstats.Parent = player
task.defer(function()
while task.wait(100) do
characterdata:Set(characterdata)
end
end)
end)
Players.PlayerRemoving:Connect(function(player)
local characterstore = DataStore2("char",player)
local characterdata = characterstore:Get()
characterstore:Set(characterdata)
print(characterdata)
end)
EDIT: i forgot to add the hat giver
task.wait(10)
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)
DataStore2.Combine("loldata","char")
local player = game.Players.pumpkyrofl
local characterstore = DataStore2("char",player)
local characterdata = characterstore:Get()
workspace.duck.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildOfClass("Humanoid") then
local clone = table.clone(characterdata)
clone[1] = game.ReplicatedFirst.Hats.RubberDuckie
characterstore:Set(clone)
end
end)
workspace.none.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildOfClass("Humanoid") then
local clone = table.clone(characterdata)
clone[1] = game.ReplicatedFirst.Hats.Hat
characterstore:Set(clone)
end
end)