You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve?
I want my skins to appear after death/character switch as well. -
What is the issue?
Everything works fine except if you get back into the main menu, the skins disappear, which is obviously not intended. -
What solutions have you tried so far?
Tried fixing it myself, I might be blind though since I usually find errors in my script.
Good to know: My menu doesn’t use ResetOnSpawn, I clone it into the player after the player died since it doesn’t work otherwise. I don’t know what else to say, idk whether I need one more loop or a character added feature for the skins to appear. Any help is appreciated!
Script is parent of the template for the skins
local player = game.Players.LocalPlayer
-- Functions
function createShopTemplate(itemName,itemPrice,object,parent)
local template = script:WaitForChild("Template"):Clone()
template.Name = itemName
template.ItemName.Text = itemName
template.BuyButton.Text = itemPrice .." Coins"
template.Price.Text = object:GetAttribute("Rarity")
local previewedObject = object:Clone(); previewedObject.Parent = template.ViewportFrame
local ori = 0
local cam = Instance.new("Camera"); cam.Parent = template.ViewportFrame
cam.CFrame = CFrame.new(object.Handle.Position + (object.Handle.CFrame.lookVector * 2), object.Handle.Position)
template.ViewportFrame.CurrentCamera = cam
template.Parent = parent
spawn(function()
while wait() do
ori += 1
previewedObject.Handle.Orientation = Vector3.new(0,ori,0)
end
end)
return template
end
game.ReplicatedStorage.Events.SendData.OnClientEvent:Connect(function()
-- Sort
local orderedSkins = {}
for i,v in pairs(game.ReplicatedStorage.Skins:GetChildren()) do
table.insert(orderedSkins,v)
end
table.sort(orderedSkins,function(a,b) return a:GetAttribute("Price") < b:GetAttribute("Price") end)
-- Shop
for i,v in pairs(orderedSkins) do
local frame = createShopTemplate(v.Name,v:GetAttribute("Price"),v,script.Parent.SkinStore.ScrollingFrame)
if player.Inventory:FindFirstChild(v.Name) then
frame.BuyButton.Text = "Owned"
--frame.BuyButton.Text = "Equip"
frame.BuyButton.BackgroundColor3 = Color3.fromRGB(255,255,0)
frame.BuyButton.BorderColor3 = Color3.fromRGB(255,255,200)
end
frame.BuyButton.MouseButton1Click:Connect(function()
local result = game.ReplicatedStorage.Events.Buy:InvokeServer(v.Name)
if result == "Brought" then
frame.BuyButton.Text = "Owned"
--frame.BuyButton.Text = "Equip"
frame.BuyButton.BackgroundColor3 = Color3.fromRGB(255,255,0)
frame.BuyButton.BorderColor3 = Color3.fromRGB(255,255,200)
elseif result == "NotEnough" then
frame.BuyButton.Text = "Not Enough Coins!"
frame.BuyButton.BackgroundColor3 = Color3.fromRGB(255, 62, 55)
wait(1)
frame.BuyButton.Text = v:GetAttribute("Price") .." Coins"
frame.BuyButton.BackgroundColor3 = Color3.fromRGB(0,255,0)
end
end)
end
end)
DataStore script
local DataStore2 = require(1936396537)
local defaultValue = 250
game.Players.PlayerAdded:Connect(function(plr)
local coinsDataStore = DataStore2("coins", plr)
local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"
local coins = Instance.new("IntValue", leaderstats)
coins.Name = "Coins"
local function coinUpdate(updatedValue)
coins.Value = coinsDataStore:Get(updatedValue)
end
coinUpdate(defaultValue)
coinsDataStore:OnUpdate(coinUpdate)
end)
DataStore2.Combine("Key","Coins","Inventory")
-- Default Values
local defaultInventory = {}
--local defaultEquippedWeapon = ""
game.Players.PlayerAdded:Connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
-- Setup
local coins = player:FindFirstChild("leaderstats").Coins
local inventory = Instance.new("Folder",player); inventory.Name = "Inventory"
--local equippedWeapon = Instance.new("StringValue",player); equippedWeapon.Name = "EquippedWeapon"
-- Get DataStores
local coinsDataStore = DataStore2("coins",player)
local inventoryStore = DataStore2("Inventory",player)
--local equippedWeaponStore = DataStore2("EquippedWeapon",player)
-- Functions
local function updateCoins(newValue)
coins.Value = newValue
end
local function updateInventory(newTable)
-- Clear
for i,v in pairs(inventory:GetChildren()) do v:Destroy() end
-- Make a new one
for i,v in pairs(newTable) do
local itemValue = Instance.new("BoolValue",inventory); itemValue.Name = v
end
end
--local function updateEquippedWeapon(newString)
--equippedWeapon.Value = newString
-- Clear
--for i,v in pairs(player.Backpack:GetChildren()) do if v:IsA("Tool") then v:Destroy() end end
--for i,v in pairs(player.StarterGear:GetChildren()) do if v:IsA("Tool") then v:Destroy() end end
--for i,v in pairs(char:GetChildren()) do if v:IsA("Tool") then v:Destroy() end end
-- Equip them
--if game.ReplicatedStorage.Weapons:FindFirstChild(equippedWeapon.Value) then
--game.ReplicatedStorage.Weapons:FindFirstChild(equippedWeapon.Value):Clone().Parent = player.Backpack
--game.ReplicatedStorage.Weapons:FindFirstChild(equippedWeapon.Value):Clone().Parent = player.StarterGear
--end
--end
-- Call functions right away
updateCoins(coinsDataStore:Get(defaultValue))
updateInventory(inventoryStore:Get(defaultInventory))
--updateEquippedWeapon(equippedWeaponStore:Get(defaultEquippedWeapon))
-- Call functions for updates
coinsDataStore:OnUpdate(updateCoins)
inventoryStore:OnUpdate(updateInventory)
--equippedWeaponStore:OnUpdate(updateEquippedWeapon)
game.ReplicatedStorage.Events.SendData:FireClient(player)
end)
game.ReplicatedStorage.Events.Buy.OnServerInvoke = function(player,itemName)
local char = player.Character
-- Get their data Store
local coinsDataStore = DataStore2("coins",player)
local inventoryStore = DataStore2("Inventory",player)
--local equippedWeaponStore = DataStore2("EquippedWeapon",player)
local item, inInventory
local newTable = {}
item = game.ReplicatedStorage.Skins:FindFirstChild(itemName)
if player.Inventory:FindFirstChild(itemName) then inInventory = true end
if item and item:GetAttribute("Price") then
if not inInventory then
-- If the player brought the item
if player.leaderstats.Coins.Value >= item:GetAttribute("Price") then
coinsDataStore:Increment(-item:GetAttribute("Price"))
local itemValue = Instance.new("BoolValue",player.Inventory); itemValue.Name = item.Name
for i,v in pairs(player.Inventory:GetChildren()) do table.insert(newTable,v.Name) end
inventoryStore:Set(newTable)
return "Brought"
else
return "NotEnough"
end
--else
--if player.EquippedWeapon.Value ~= item.Name then
--equippedWeaponStore:Set(item.Name)
--return "Equip"
--else
--equippedWeaponStore:Set("")
--return "Unequip"
--end
end
end
end
Video for demonstration: Layout disappearing
Image shows the LocalScript which is parent of the template in MenuGui.