the line of code giving the item purchased by the player via the buy button gives me that error :attempt to index nil with ‘Clone’ and I don’t know why.
local player = game.Players.LocalPlayer
local backpack = player.Backpack
price = 20
tool = game.ServerStorage:FindFirstChild("Pilule")
script.Parent.MouseButton1Click:Connect(function()
if player.leaderstats.Dollars.Value >= 20 then
player.leaderstats.Dollars.Value -= price
local b = tool:Clone()
b.Parent = player.Backpack
end
end)
it works I can buy and collect the item. Strangely enough, however, any item I buy with a script doesn’t work. For example, a medical kit that restores life by clicking when you equip it doesn’t work once it’s bought and in hand, whereas if I put my tool in StarterPack, the medical kit works. As if all scripts were deactivated from purchased items.
1.you cant access serverstorage from client
2.notice you change the player stats on client meaning it wont replicate to server and can be problematic, use a remote event to handle the purchase on the server side.
As @Maxime_360 and @Valkyrop mentioned, you are typing the code in a LocalScript which means the code is running client-side. The client cannot access items stored in the ServerStorage, that is why it returns nil when referencing the tool. You should consider using a RemoteEvent to add it from the server.
game.ReplicatedStorage.ToolEvents.HealthDrinkEvent.OnServerEvent:Connect(function(player)
if player.leaderstats.Coins.Value >= 6 then
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 6
game.ServerStorage.Tools.HealthDrink:Clone().Parent = player.Backpack
end
end)
The script in ServerScriptService could have many tools in the same script.
All one right after eachother. Each with their own event in ReplicatedStorage.