local DataStoreService = game:GetService("DataStoreService")
local toolDataStore = DataStoreService:GetDataStore("PlayerTools")
local Time = nil
if Player:FindFirstChild("leaderstats") then
Time = Player.leaderstats:FindFirstChild("Time")
end
local purchasedTools = {}
local equippedTools = {}
-- Load purchased tools from DataStore
local function loadPlayerTools()
local success, data = pcall(function()
return toolDataStore:GetAsync(Player.UserId)
end)
if success and data then
purchasedTools = data.purchasedTools or {}
equippedTools = data.equippedTools or {}
else
warn("Failed to load tools for " .. Player.Name)
end
end
-- Equip tools, ensuring no duplicates
local function equipTools()
-- Clear existing tools in the backpack to prevent duplication
for _, tool in ipairs(Player.Backpack:GetChildren()) do
tool:Destroy()
end
-- Equip only saved tools
for _, toolName in ipairs(equippedTools) do
if table.find(purchasedTools, toolName) then
local tool = game.ReplicatedStorage:WaitForChild("Tools"):FindFirstChild(toolName)
if tool then
local newTool = tool:Clone()
newTool.Parent = Player.Backpack
end
end
end
end
-- Save purchased tools to DataStore
local function savePlayerTools()
local success, err = pcall(function()
toolDataStore:SetAsync(Player.UserId, {
purchasedTools = purchasedTools,
equippedTools = equippedTools
})
end)
if not success then
warn("Failed to save tools for " .. Player.Name .. ": " .. err)
end
end
-- Load and equip initial tools
loadPlayerTools()
equipTools()
Player.CharacterAdded:Connect(equipTools)
-- Tool prices and order logic
local toolPrices = {}
for _, tool in ipairs(game.ReplicatedStorage:WaitForChild("Tools"):GetChildren()) do
if tool:IsA("Tool") then
local Price = tool:FindFirstChild("Price") and tool.Price.Value or 100
table.insert(toolPrices, {tool = tool, price = Price})
end
end
table.sort(toolPrices, function(a, b)
return a.price < b.price
end)
-- Generate shop UI and button logic
-- Generate shop UI and button logic
for index, toolInfo in ipairs(toolPrices) do
local tool = toolInfo.tool
local Price = toolInfo.price
local ToolFrame = script.ToolFrame:Clone()
ToolFrame.Size = UDim2.new(1, 100, 0, 50)
ToolFrame.BuyButton.Price.Text = Price .. "⏱"
ToolFrame.ToolName.Text = tool.Name
ToolFrame.ImageLabel.Image = tool.TextureId
ToolFrame.Parent = script.Parent.ShopFrame.Frame
local function updateButtonText()
if table.find(purchasedTools, tool.Name) then
local isEquipped = table.find(equippedTools, tool.Name) ~= nil
ToolFrame.BuyButton.Price.Text = isEquipped and "Unequip" or "Equip"
else
ToolFrame.BuyButton.Price.Text = Price .. "⏱"
end
end
-- Initial button state
updateButtonText()
ToolFrame.BuyButton.MouseButton1Click:Connect(function()
-- Check if the player can buy this tool (must own all previous tools)
local canPurchase = index == #purchasedTools + 1
if table.find(purchasedTools, tool.Name) then
-- Equip or Unequip logic
local isEquipped = table.find(equippedTools, tool.Name) ~= nil
if isEquipped then
local toolInstance = Player.Backpack:FindFirstChild(tool.Name)
if toolInstance then
toolInstance:Destroy()
end
table.remove(equippedTools, table.find(equippedTools, tool.Name))
else
local toolToEquip = game.ReplicatedStorage:WaitForChild("Tools"):FindFirstChild(tool.Name)
if toolToEquip then
local newTool = toolToEquip:Clone()
newTool.Parent = Player.Backpack
table.insert(equippedTools, tool.Name)
end
end
elseif canPurchase then
-- Buying logic
if Time and Time.Value >= Price then
Time.Value -= Price
table.insert(purchasedTools, tool.Name)
-- Equip tool after purchase
local newTool = tool:Clone()
newTool.Parent = Player.Backpack
table.insert(equippedTools, tool.Name)
else
warn("Not enough Time to purchase " .. tool.Name)
end
else
warn("You must buy tools in order!")
end
-- Update button text and save tools
updateButtonText()
savePlayerTools()
end)
end
-- Save tools when player leaves
game.Players.PlayerRemoving:Connect(function(p)
if p == Player then
savePlayerTools()
end
end)
-- Save tools periodically
while wait(300) do -- Saves every 5 minutes
savePlayerTools()
end
Idk why its not using my Time leaderstat that i already have.
Typo in your code, you actually need to write
Time = Player.leaderstats:FindFirstChild("Time").Value
as i assume that the child Time
is a numberValue or any equivalent…
Yes its a number value and a int value
My bad I read this a little too fast. When the code ran, did you see any warn
output?
Your good man in output yes, it shows Not enough Time to purchase (whatever tool im trying to buy)
Alright, last thing I’ll ask you is to add a print(Time)
right after your line
elseif canPurchase then
-- Buying logic
-- right here, add: print(Time)
- if the output is
nil
, then you didn’t retrieve correctly your intValue inleaderstats
- if the output is not
nil
, it means you most likely have accessed your instance’s value, but that the conditionTime.Value >= Price
is false.
Maybe I didn’t understand your problem correctly, but this is the only part of the code that you use your Time
variable so I assume that the problem comes most-likely from the second point I’ve made. Hope it helps.
Yeah its showing “nil” in output
Alright, let’s try changing the above code to
Time = Player:WaitForChild("leaderstats"):WaitForChild("Time")
-- for debugging purposes, you could add a "print('done!')" right after this line of code
That was it man! Thank you. Issue solved
Glad that I’ve been useful
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.