I tried to create a selling system that checks if a tool is in your inventory and then changes your leaderstats coins value accordingly, to around 500 per item sold, what is wrong with my script? It’s a local script inside of a sellpart in Workspace
local player = game.Players.LocalPlayer
local Backpack = player:WaitForChild("Backpack")
local FindTool = Backpack:FindFirstChild("ClassicSword")
local coins = player.leaderstats.Coins
local price = 500
local sellpart = script.Parent
sellpart.ClickDetector.MouseClick:Connect(function()
if FindTool then
FindTool:Destroy()
coins.Value = 500
end
end)
I don’t think changing the value of your leaderstats on the client would reflect properly. You would need to update the money on the server, and in this case you can just send the value through a remote event.
If I were you, I would only pass the item name via the remote event. If you instead pass the amount it sells for, exploiters could potentially exploit that remote event. The server should then check if the player actually has the item in their backpack/character and then give them the right number of coins based on the item they’re trying to sell.
Put this in a script (ServerScript) and it should work.
local sellPart = script.Parent
-- When the clickdetector is pressed.
sellPart.ClickDetector.MouseClick:Connect(function(player: Player)
local tool = player.Backpack:FindFirstChild("ClassicSword")
-- If the sword isn't found, then do nothing.
if not tool then
return
end
-- If the sword is found, then destory the tool and add x amount of coins.
tool:Destroy()
player.leaderstats.Coins.Value += 500
-- ^ You could check if the leaderstats folder exists. ^
end)
to do it on server script you would want this so you can get the player.
local price = 500
local sellpart = script.Parent
sellpart.ClickDetector.MouseClick:Connect(function(player)
local Backpack = player:WaitForChild("Backpack")
local FindTool = Backpack:FindFirstChild("ClassicSword")
local coins = player.leaderstats.Coins
if FindTool then
FindTool:Destroy()
coins.Value += 500
end
end)