Hey everyone. I have been stuck on a shop saving datastore script for hours, and I don’t want to have to discontinue my game because of a datastore issue.
The shop gui works fine, but when I rejoin, the shop doesn’t save. (There is always a default classic sword that everyone will get)
The datastore script:
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = Player
leaderstats.Name = "leaderstats"
local Level = Instance.new("IntValue")
Level.Parent = Player
Level.Name = "Level"
local Xp = Instance.new("IntValue")
Xp.Parent = Player
Xp.Name = "Xp"
local MaxXp = Instance.new("IntValue")
MaxXp.Parent = Player
MaxXp.Name = "MaxXp"
MaxXp.Value = 100
local Money = Instance.new("IntValue")
Money.Parent = Player
Money.Name = "Money"
local Chance = Instance.new("IntValue")
Chance.Parent = Player
Chance.Name = "Chance"
Chance.Value = 10
local Weapon = Instance.new("StringValue")
Weapon.Parent = Player
Weapon.Name = "Weapon"
local Effect = Instance.new("StringValue")
Effect.Parent = Player
Effect.Name = "Effect"
local UnlockedWeapons = Instance.new("Folder")
UnlockedWeapons.Parent = Player
UnlockedWeapons.Name = "UnlockedWeapons"
local UnlockedEffects = Instance.new("Folder")
UnlockedEffects.Parent = Player
UnlockedEffects.Name = "UnlockedEffects"
--[[DATASTORES]]--
local MoneyStore = Datastore2("Money", Player)
local LevelStore = Datastore2("Level", Player)
local XpStore = Datastore2("Xp", Player)
local MaxXpStore = Datastore2("MaxXp", Player)
local UnlockedWeaponsStore = Datastore2("UnlockedWeapons", Player)
local UnlockedEffectsStore = Datastore2("UnlockedEffects", Player)
local EffectStore = Datastore2("Effect", Player)
local WeaponStore = Datastore2("Weapon", Player)
local function UpdateMoney(MoneyData)
Money.Value = MoneyStore:Get(MoneyData)
end
local function UpdateLevel(LevelData)
Level.Value = LevelStore:Get(LevelData)
end
local function UpdateXp(XpData)
Xp.Value = XpStore:Get(XpData)
end
local function UpdateMaxXp(MaxXpData)
MaxXp.Value = MaxXpStore:Get(MaxXpData)
end
local function UpdateEffect(EffectData)
Effect.Value = EffectStore:Get(EffectData)
end
local function UpdateWeapon(WeaponData)
Weapon.Value = WeaponStore:Get(WeaponData)
end
local function UpdateUnlockedWeapons(UnlockedWeaponsData)
UnlockedWeapons:ClearAllChildren()
for i, v in pairs(UnlockedWeaponsData) do
print(i, v)
local NewValue = Instance.new("StringValue", UnlockedWeapons)
NewValue.Name = v
end
end
UpdateMoney(0)
MoneyStore:OnUpdate(UpdateMoney)
UpdateLevel(1)
LevelStore:OnUpdate(UpdateLevel)
UpdateXp(0)
XpStore:OnUpdate(UpdateXp)
UpdateMaxXp(100)
LevelStore:OnUpdate(UpdateMaxXp)
UpdateEffect("OOF!")
EffectStore:OnUpdate(UpdateEffect)
UpdateWeapon()
WeaponStore:OnUpdate(UpdateWeapon)
UpdateUnlockedWeapons(UnlockedWeaponsStore:Get(UnlockedWeaponsTable))
UnlockedWeaponsStore:OnUpdate(UpdateUnlockedWeapons)
end)
(The datastore name is “UnlockedWeaponsStore” and the folder containing the values is “UnlockedWeapons”
Here is the localscript for the shop:
local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")
local UnlockedWeapons = Player:WaitForChild("UnlockedWeapons")
local Weapon = Player:WaitForChild("Weapon")
local Shop = script.Parent
local Scrolling = Shop:WaitForChild("ScrollingFrame")
local ConfirmPurchase = Shop.Parent:WaitForChild("ConfirmPurchase")
local CloseShop = Shop:WaitForChild("CloseShop")
local SideButtons = Shop.Parent.Parent:WaitForChild("SideButtons")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Shared = ReplicatedStorage:WaitForChild("Shared")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local BuyWeapon = Remotes:WaitForChild("BuyWeapon")
local EquipWeapon = Remotes:WaitForChild("EquipWeapon")
local WeaponModels = ReplicatedStorage:WaitForChild("WeaponModels")
local WeaponModule = require(Shared:WaitForChild("WeaponShop"))
local Module3D = require(Shared:WaitForChild("Module3D"))
local SelectedButton
for i, v in pairs(WeaponModule.WeaponList) do
local Template = Shop.WeaponsTemplate:Clone()
Template.Visible = true
Template.PriceText.Text = tostring("$"..v)
Template.WeaponText.Text = i
Template.WeaponValue.Value = i
Template.WeaponPrice.Value = v
Template.Parent = Scrolling
Template.LayoutOrder = v --layout order for the cost
Scrolling.UIGridLayout.SortOrder = Enum.SortOrder.LayoutOrder
Scrolling.UIGridLayout:ApplyLayout()
if WeaponModels[i] then
local WeaponModel = WeaponModels[i]:Clone()
local Model3D = Module3D:Attach3D(Template.ViewportFrame, WeaponModel)
Model3D:SetDepthMultiplier(1)
Model3D.Camera.FieldOfView = 5
Model3D.Visible = true
game:GetService("RunService").RenderStepped:Connect(function()
Model3D:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
end)
if UnlockedWeapons:FindFirstChild(Template.WeaponValue.Value) then --check if player has weapon
Template.AlreadyBought.Value = true
Template.OwnedText.Visible = true
end
if Template.WeaponPrice.Value == 0 then --Free weapon
local Result = BuyWeapon:InvokeServer(Template.WeaponValue.Value, Template.WeaponPrice.Value, true)
if Result == "Bought" then
Template.AlreadyBought.Value = true
Template.OwnedText.Visible = false
Template.PriceText.Visible = false
Template.EquippedText.Visible = true
ConfirmPurchase.Visible = false
for i, v in pairs(SideButtons:GetChildren()) do
v.Visible = true
end
end
end
Template.MouseButton1Down:Connect(function()
SelectedButton = Template
if Template.AlreadyBought.Value == false then
ConfirmPurchase.Visible = true
ConfirmPurchase.Message.Text = "Are you sure you want to buy the "..Template.WeaponValue.Value.." for $"..Template.WeaponPrice.Value.."?"
Shop.Visible = false
CloseShop.Visible = false
for i, v in pairs(SideButtons:GetChildren()) do
v.Visible = false
end
local Connection1
Connection1 = ConfirmPurchase.Yes.MouseButton1Down:Connect(function()
local Result = BuyWeapon:InvokeServer(Template.WeaponValue.Value, Template.WeaponPrice.Value)
if Result == "Bought" then
Connection1:Disconnect()
for i, v in pairs(Scrolling:GetChildren()) do
if v:FindFirstChild("IsTemplate") and UnlockedWeapons:FindFirstChild(v.WeaponValue.Value) and v ~= Template then
v.OwnedText.Visible = true
v.EquippedText.Visible = false
end
end
Template.OwnedText.Visible = false
Template.EquippedText.Visible = true
Template.PriceText.Visible = false
Template.AlreadyBought.Value = true
ConfirmPurchase.Visible = false
Shop.Visible = true
CloseShop.Visible = true
for i, v in pairs(SideButtons:GetChildren()) do
v.Visible = true
end
print(Template.WeaponValue.Value)
end
end)
ConfirmPurchase.No.MouseButton1Down:Connect(function()
Connection1:Disconnect()
ConfirmPurchase.Visible = false
Shop.Visible = true
CloseShop.Visible = true
for i, v in pairs(SideButtons:GetChildren()) do
v.Visible = true
end
end)
else
local Result = EquipWeapon:InvokeServer(Template.WeaponValue.Value)
if Result == "Equipped" then
for i, v in pairs(Scrolling:GetChildren()) do
if v:FindFirstChild("IsTemplate") and UnlockedWeapons:FindFirstChild(v.WeaponValue.Value) and v ~= Template then
print(v.WeaponValue.Value)
v.OwnedText.Visible = true
v.EquippedText.Visible = false
end
end
Template.OwnedText.Visible = false
Template.EquippedText.Visible = true
Template.PriceText.Visible = false
end
end
end)
end
end
repeat wait() until #UnlockedWeapons:GetChildren() >= 1
for i, Template in pairs(Shop.ScrollingFrame:GetChildren()) do
if Template:IsA("TextButton") then
if UnlockedWeapons:FindFirstChild(Template.WeaponValue.Value) and Template.WeaponPrice.Value ~= 0 then --check if player has weapon
Template.AlreadyBought.Value = true
Template.OwnedText.Visible = true
Template.PriceText.Visible = false
end
end
end
Any help is appreciated, I followed some videos but they didn’t work for me. Thanks.