A example: A Players buys a Item and equipp it, then its saved in the Data, later i delete the Item from the game, the game cant find the Item from the Data Store or/and in the Items and the Game breaks. How can i make a script that unequip the Item when not found the Item?
(If u need more scripts to find the “Bug”, please tell me)
Here is the script:
Core Gui(Local Script):
local availableTools = game.ReplicatedStorage:WaitForChild("GetTools"):InvokeServer()
local mainFrame = script.Parent:WaitForChild("MainFrame")
local safeArea = mainFrame:WaitForChild("SafeArea")
local itemInformation = safeArea:WaitForChild("ItemInformation")
local infoFrame = itemInformation.InfoFrame
local selectedItem = itemInformation.SelectedItem
local equippedItem = itemInformation.EquippedItem
local numberOfItems = #availableTools
local itemFrame = safeArea.ItemFrame
--
local buyButton = infoFrame.BuyButton
local equippedItemViewport = script.Parent.DetailFrame:WaitForChild("EquippedItemViewport")
local itemViewport = itemInformation.ItemViewport
--
game.ReplicatedStorage.SendEquipped.OnClientEvent:Connect(function(equipped)
equippedItem.Value = equipped
if equippedItem.Value ~= "" then
local fakeCam = Instance.new("Camera")
fakeCam.Parent = equippedItemViewport
local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(equippedItem.Value.."Handle"):Clone()
handle.Parent = equippedItemViewport
equippedItemViewport.CurrentCamera = fakeCam
fakeCam.CFrame = handle.CameraCFrame.Value
end
end)
local PADDING_X = 0.02
local DROPDOWN_Y = 0.2
local DROPDOWN_X = 0.25
local item1 = itemFrame:WaitForChild("Item1")
local box
local numRows = 1
for i = 1,numberOfItems,1 do
if i == 1 then
box = item1
else
box = item1:Clone()
box.Name = "Item"..i
box.Parent = itemFrame
if (i-1) / (4*numRows) == 1 then
-- New row
numRows = numRows + 1
box.Position = UDim2.new(PADDING_X,0,box.Position.Y.Scale,0) + UDim2.new(0,0,DROPDOWN_Y*(numRows - 1))
else
-- Add to the X only
box.Position = itemFrame["Item"..(i-1)].Position + UDim2.new(DROPDOWN_X,0,0,0)
end
end
box.MouseButton1Click:Connect(function()
for _, v in pairs(itemViewport:GetChildren()) do
if not v:IsA("Frame") then
v:Destroy()
end
end
local itemViewportCam = Instance.new("Camera")
itemViewportCam.Parent = itemViewport
local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(availableTools[i][1].."Handle"):Clone()
handle.Parent = itemViewport
itemViewport.CurrentCamera = itemViewportCam
itemViewportCam.CFrame = handle.CameraCFrame.Value
local owned = game.ReplicatedStorage.ItemCheck:InvokeServer(availableTools[i][1])
if equippedItem.Value == availableTools[i][1] then
infoFrame.Cash.Text = "Owned"
infoFrame.BuyButton.Text = "Unequip"
elseif owned == true then
infoFrame.Cash.Text = "Owned"
infoFrame.BuyButton.Text = "Equip"
else
infoFrame.BuyButton.Text = "Buy"
infoFrame.Cash.Text = "$"..availableTools[i][2]
end
infoFrame.ItemName.Text = availableTools[i][1]
selectedItem.Value = availableTools[i][1]
for _, v in pairs(itemFrame:GetChildren()) do
if v:IsA("ImageButton") then
v.BorderSizePixel = 0
end
end
itemFrame["Item"..i].BorderSizePixel = 2
end)
local fakeCam = Instance.new("Camera")
fakeCam.Parent = box.VPF
local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(availableTools[i][1].."Handle"):Clone()
handle.Parent = box.VPF
box.VPF.CurrentCamera = fakeCam
fakeCam.CFrame = handle.CameraCFrame.Value
itemFrame["Item"..i].ItemName.Text = availableTools[i][1]
end
buyButton.MouseButton1Click:Connect(function()
local result = game.ReplicatedStorage.PurchaseItem:InvokeServer(selectedItem.Value)
if result == true then
buyButton.BackgroundColor3 = Color3.fromRGB(42,149,42)
buyButton.Text = "Bought!"
wait(0.5)
buyButton.Text = "Equip"
buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
elseif result == "NotEnoughMoney" then
buyButton.BackgroundColor3 = Color3.fromRGB(204,31,55)
buyButton.Text = "Not enough Money!"
wait(0.5)
buyButton.Text = "Buy"
buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
elseif result == "Equipped" then
equippedItem.Value = selectedItem.Value
buyButton.BackgroundColor3 = Color3.fromRGB(42,149,42)
buyButton.Text = "Equipped!"
wait(0.5)
buyButton.Text = "Unequip"
buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
elseif result == "Unequipped" then
equippedItem.Value = ""
buyButton.BackgroundColor3 = Color3.fromRGB(42,149,42)
buyButton.Text = "Unequipped"
wait(0.5)
buyButton.Text = "Equip"
buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
end
end)
if equippedItem.Value ~= "" then
local fakeCam = Instance.new("Camera")
fakeCam.Parent = equippedItemViewport
local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(equippedItem.Value.."Handle"):Clone()
handle.Parent = equippedItemViewport
equippedItemViewport.CurrentCamera = fakeCam
fakeCam.CFrame = handle.CameraCFrame.Value
end
equippedItem:GetPropertyChangedSignal("Value"):Connect(function()
if equippedItem.Value ~= "" then
for _, v in pairs(equippedItemViewport:GetChildren()) do
if not v:IsA("Folder") then
v:Destroy()
end
end
local fakeCam = Instance.new("Camera")
fakeCam.Parent = equippedItemViewport
local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(equippedItem.Value.."Handle"):Clone()
handle.Parent = equippedItemViewport
equippedItemViewport.CurrentCamera = fakeCam
fakeCam.CFrame = handle.CameraCFrame.Value
else
for _, v in pairs(equippedItemViewport:GetChildren()) do
if not v:IsA("Folder") then
v:Destroy()
end
end
end
end)
1 Serverscript:
game.ReplicatedStorage:WaitForChild("GetTools").OnServerInvoke = function(player)
local items = {}
for _, object in pairs(game.ServerStorage:WaitForChild("Items"):GetChildren()) do
local itemProperties = {object.Name,object.Price.Value}
table.insert(items,itemProperties)
end
return items
end
game.ReplicatedStorage:WaitForChild("ItemCheck").OnServerInvoke = function(player,itemName)
if game.ServerStorage.PlayerData:FindFirstChild(player.Name).Inventory:FindFirstChild(itemName) then
return true
else
return false
end
end
game.ReplicatedStorage:WaitForChild("PurchaseItem").OnServerInvoke = function(player,itemName)
local money = player.leaderboardsmoney.Money
local item = game.ServerStorage.Items:FindFirstChild(itemName)
if item then
-- Item exists
if game.ServerStorage.PlayerData[player.Name].Inventory:FindFirstChild(itemName) then
if game.ServerStorage.PlayerData[player.Name].Equipped.Value ~= itemName then
-- Currently unequipped
game.ServerStorage.PlayerData[player.Name].Equipped.Value = itemName
return "Equipped"
else
game.ServerStorage.PlayerData[player.Name].Equipped.Value = ""
return "Unequipped"
end
end
if money.Value >= item.Price.Value then
money.Value = money.Value - item.Price.Value
local itemValue = Instance.new("ObjectValue")
itemValue.Name = itemName
itemValue.Parent = game.ServerStorage.PlayerData[player.Name].Inventory
return true
else
return "NotEnoughMoney"
end
else
return "NoItem"
end
end
2 Serverscript:
local dataStores = game:GetService("DataStoreService"):GetDataStore("MoneyDataStore")
local defaultCash = 10
local playersLeft = 0
game.Players.PlayerAdded:Connect(function(player)
playersLeft = playersLeft + 1
local leaderboardsmoney = Instance.new("Folder")
leaderboardsmoney.Name = "leaderboardsmoney"
leaderboardsmoney.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = 0
money.Parent = leaderboardsmoney
local playerData = Instance.new("Folder")
playerData.Name = player.Name
playerData.Parent = game.ServerStorage.PlayerData
local equipped = Instance.new("StringValue")
equipped.Name = "Equipped"
equipped.Parent = playerData
local inventory = Instance.new("Folder")
inventory.Name = "Inventory"
inventory.Parent = playerData
player.CharacterAdded:Connect(function(character)
---- character.Humanoid.Walkspeed = 50 ---- (Don´t Work!/Dont need!)
character.Humanoid.Died:Connect(function()
-- Whenever somebody dies, this event will run
if character.Humanoid and character.Humanoid:FindFirstChild("creator") then
game.ReplicatedStorage.KillFeed.Value = tostring(character.Humanoid.creator.Value).." 🔪 "..player.Name
end
if character:FindFirstChild("GameTag") then
character.GameTag:Destroy()
end
player:LoadCharacter()
end)
end)
-- Data Stores
local player_data
local weaponsData
local equippedData
pcall(function()
player_data = dataStores:GetAsync(player.UserId.."-Money") --1736284-Money
end)
pcall(function()
weaponsData = dataStores:GetAsync(player.UserId.."-Weapons") --1736284-Money
end)
pcall(function()
equippedData = dataStores:GetAsync(player.UserId.."-EquippedValue") --1736284-Money
end)
if player_data ~= nil then
-- Player has saved data, load it in
money.Value = player_data
else
-- New player
money.Value = defaultCash
end
if weaponsData then
for _, weapon in pairs(weaponsData) do
if game.ServerStorage.Items:FindFirstChild(weapon) then
local weaponClone = game.ServerStorage.Items[weapon]:Clone()
weaponClone.Parent = inventory
print(weapon.." loaded in!")
end
end
if equippedData then
equipped.Value = equippedData
player:WaitForChild("PlayerGui")
game.ReplicatedStorage.SendEquipped:FireClient(player,equippedData)
end
else
print("No weapons data")
end
end)
local bindableEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
dataStores:SetAsync(player.UserId.."-Money",player.leaderboardsmoney.Money.Value)
end)
pcall(function()
local weapons = game.ServerStorage.PlayerData[player.Name].Inventory:GetChildren()
local weaponsTable = {}
for _, v in pairs(weapons) do
table.insert(weaponsTable,v.Name)
end
dataStores:SetAsync(player.UserId.."-Weapons",weaponsTable)
if game.ServerStorage.PlayerData[player.Name].Equipped.Value ~= nil then
local equippedVal = game.ServerStorage.PlayerData[player.Name].Equipped
dataStores:SetAsync(player.UserId.."-EquippedValue",equippedVal.Value)
end
end)
print("Saved weapons and points")
playersLeft = playersLeft - 1
bindableEvent:Fire()
end)
game:BindToClose(function()
-- When the game is going to shutdown, do whatever is in here before it closes
while playersLeft > 0 do
bindableEvent.Event:Wait()
end
end)