Selling System - leaderstats

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.

1 Like

LocalScripts doesn’t run in the Workspace, so you’ll have to change to a server-sided script.

1 Like

Localscripts do not run in workspace.

1 Like

Well, localscripts don’t, but server scripts do.

I’m sure that’s what you meant, but this phrasing can be confusing for new devs.

3 Likes

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.

1 Like

you should use server scripts instead in workspace.

im about to try out some of this stuff

it’s indexing nil with the backpack

What would the function for that be, because I’m super confused at the moment

It’s indexing nil with the backpack once I switched it to a server script, so atleast its running somewhat but not really doing the function

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)

if you switch to server script there wouldnt be a local player, so backpack cant index nil

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)
1 Like

Thank you for pointing my error out

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.