Sell script help

help me with this sell script it’s suppose it clone a template located in server storage and place it in a gui with the player’s tool’s information but it’s just not working.

local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")

local ds = dataStoreService:GetDataStore("UNBOXING SYSTEM DATA")

local Template = serverStorage:WaitForChild("Templates"):WaitForChild("TemplateSell")

players.PlayerAdded:Connect(function(plr)

	local Cash = plr:WaitForChild("leaderstats"):WaitForChild("Cash")

	local backpack = plr.Backpack

	for _, tool in ipairs(plr.Backpack:GetDescendants()) do

		if tool:IsA("Tool") then
			local clone = Template:Clone()
			clone.Parent = game:GetService("StarterGui"):WaitForChild("InGameMenu"):WaitForChild("Popups"):WaitForChild("SellPopup"):WaitForChild("Main"):WaitForChild("ScrollingFrame")
			clone.TextLabel.Text = tool.Name
			clone.ImageLabel.Image = tool.TextureId
		end

		Template.TextButton.MouseButton1Click:Connect(function()

			local success, errorMessage = pcall(function()

				ds:RemoveAsync(tool)

				if tool.Name == "Bluesteel" or "Carbon" or "Ducky" or "Emerald" or "Frost" or "Sinister" or "Snowflake" or "Spider" then

					Cash.Value += 150

				elseif tool.Name == "Red Trident" or "Blue Trident" or "Trident" then

					Cash.Value += 300

				elseif tool.Name == "Blue" or "Green" or "Pink" or "Purple" or "Red" or "Yellow" then

					Cash.Value += 550

				elseif tool.Name == "Blank" or "Chroma" or "Galaxy" or "Magma" or "Spectrum" or "Stained" or "Tropical" then

					Cash.Value += 1000

				elseif tool.Name == "Blood Trident" or "Chroma Trident" or "Galaxy Trident" then

					Cash.Value += 1650

				end

			end)

		end)

	end

end)

6 Likes

Maybe problem in button click connection (I never tested if these works on server so uhhh maybe
I will look more but there other person replying so they may have fix

Well atleast first try printing error message if there are, if it wont print anything then problem surely in loading something

there are no error messages and i don’t know the causes of it not working i had a few but fixing those didn’t solve it

Please elaborate more on your problem i.e. what’s wrong with it.

Did you mean

if tool.Name == "Bluesteel" or tool.Name == "Carbon" or tool.Name =="Ducky" or tool.Name =="Emerald" or tool.Name == "Frost" or tool.Name == "Sinister" or tool.Name == "Snowflake" or tool.Name == "Spider" then

I suggest not to do this kind of style.


-- More readable
local cashMap = {
    Bluesteel = 150,
    Carbon = 150,
    Ducky = 150,
    Emerald = 150,
    Frost = 150,
    Sinister = 150,
    Snowflake = 150,
	Spider = 150,
    ...
}

Cash.Value += cashMap[tool.Name]
1 Like

MouseButton1Click cannot be used on the server, you should use RemoteEvents and use the MouseButton1Click on the client instead then have the server check if does the player actually have these tools then take them away and give cash

1 Like

i… don’t know how to use remote events lol

There are a ton of tutorials which can help you with it, here’s a video which may help https://www.youtube.com/watch?v=3wkPekjQvKg

RemoteEvents aren’t that complex so I believe you’ll understand them quickly!

yes well thats what I told him at start (I said that it probably don’t work on server, since I never tested I can’t know for sure but sounds logical that it wont work)

let me try to see if this works

i completely don’t understand. Is something wrong with me?

You need to create a local script inside the GUI with the ‘MouseButton1Click’ function. Then, fire the remote event through the MouseButton1Click function, and write code in the server script to respond to the event fired by the client.

Make sure the RemoteEvent is also placed in ReplicatedStorage so that it is accessible by both the server and the client

1 Like

If you want to understand how remote events work you gotta understand how the server-client relations work, i heavily suggest you learn about remote events cause they are crucial and very important for most developers, you have to know about remote events in order to script things like transactions

1 Like

problem is i don’t know which part of the code i should fire it in

you fire the remote event on the local script by doing :FireServer()

then on the server script you do remoteEvent.OnServerEvent:Connect(function(plr)

end)

idk if i wrote the code right cuz i didnt use studio but u gotta do something similar to that

Think of it as when you fire the remote event, ur basically sending a message to the server, and then on the server script you gotta write code on how to reply to that message

Also ur creating buttons through the server script, do you know if thats the right thing to do? You could just create the buttons yourself in starterGui and then make them visible/invisible

it still doesn’t work though i don’t know what’s wrong, there are no errors in the output

this is my local script:

local replicatedStorage = game:GetService("ReplicatedStorage")

--\\ TABLES //--
local Commons = {

	"Bluesteel";
	"Carbon";
	"Ducky";
	"Emerald";
	"Frost";
	"Sinister";
	"Snowflake";
	"Spider";

}

local CommonTridents = {

	"Red Trident";
	"Blue Trident";
	"Trident";

}

local Uncommons = {

	"Blue";
	"Green";
	"Pink";
	"Purple";
	"Red";
	"Yellow";

}

local Rares = {

	"Blank";
	"Chroma";
	"Galaxy";
	"Magma";
	"Spectrum";
	"Stained";
	"Tropical";

}

local RareTridents = {

	"Blood Trident";
	"Chroma Trident";
	"Galaxy Trident";

}

local Epics = {

	"Daisy";
	"Frostbite";
	"Hollow";
	"Seer";
	"Snakebite";
	"Soul Eater";

}

local EpicTridents = {

	"Hollow Trident";
	"Overseer Trident";
	"Poseidon's Trident";

}

local Legendaries = {

	"Assassin";
	"Batwing";
	"Candle";
	"Colossal";
	"Cupid";
	"Gemstone";
	"Logchopper";
	"Plasmablade";
	"Royal";

}

local LegendaryTridents = {

	"Aqua Trident";
	"Hellfork";
	"Icefork";

}

local MythicalTridents = {

	"God's Bane";
	"Heavenly Trident";

}


local Remote = replicatedStorage:WaitForChild("Remotes"):WaitForChild("SellRemote")

Remote.OnClientEvent:Connect(function(plr : Player, tool, clone)
	
	local leaderstats = plr:WaitForChild("leaderstats")
	local Cash = leaderstats:WaitForChild("Cash")
	
	clone.TextButton.MouseButton1Click:Connect(function()
		
		Remote:FireServer(tool)
		
		if tool.Name == Commons then

			Cash.Value += 150

		elseif tool.Name == CommonTridents then

			Cash.Value += 300

		elseif tool.Name == Uncommons then

			Cash.Value += 550

		elseif tool.Name == Rares then

			Cash.Value += 1000

		elseif tool.Name == RareTridents then

			Cash.Value += 1650

		elseif tool.Name == Epics then

			Cash.Value += 2000

		elseif tool.Name == EpicTridents then

			Cash.Value += 2500

		elseif tool.Name == Legendaries then

			Cash.Value += 3250

		elseif tool.Name == LegendaryTridents then

			Cash.Value += 3750

		elseif tool.Name == MythicalTridents then

			Cash.Value += 6750

		end

	end)
	
end)

here’s the modified server script:

--\\ SERVICES //--
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local replicatedStorage = game:GetService("ReplicatedStorage")

--\\ DATASTORE //--
local ds = dataStoreService:GetDataStore("UNBOXING SYSTEM DATA")

--\\ TEMPLATE LOCATIONS //--
local Template = serverStorage:WaitForChild("Templates"):WaitForChild("TemplateSell")

--\\REMOTES//--
local Remotes = replicatedStorage:WaitForChild("Remotes")
local SellRemote = Remotes:WaitForChild("SellRemote")

--\\ MAIN FUCTIONS //--
players.PlayerAdded:Connect(function(plr)

	local Cash = plr:WaitForChild("leaderstats"):WaitForChild("Cash")

	local backpack = plr.Backpack

	for _, tool in ipairs(plr.Backpack:GetChildren()) do

		if tool:IsA("Tool") then
			local clone = Template:Clone()
			clone.Parent = game:GetService("StarterGui"):WaitForChild("InGameMenu"):WaitForChild("Popups"):WaitForChild("SellPopup"):WaitForChild("Main"):WaitForChild("ScrollingFrame")
			clone.TextLabel.Text = tool.Name
			clone.ImageLabel.Image = tool.TextureId
			
			SellRemote:FireClient(plr, tool, clone)
			
			SellRemote.OnServerEvent:Connect(function(tool)
				
				ds:RemoveAsync(tool)
				
			end)
			
		end
	end

end)

i’m not sure if firing the client from the server right away is a good idea but wjhatever

and you’re modifying the value of the cash through the local script which is a bad idea cause you gotta do it on the server
where is even your local script? inside a button?

and you dont need to rename the clone and its image label cause :Clone() automatically does that
Are you sure you wrote this? cause your script doesnt make alot of sense

i’m bad at scripting alright excuse my very bad scripting and the local script is in a frame

i didnt attack you for that lol im bad at scripting as well sometimes

how about you put all the tools you wanna sell inside a folder in replicated storage (or replicated storage itself) and then make the local script, not the server, create buttons for you for each tool inside replicated storage and with MouseButton1Click

and then you make a server script thats gonna handle when the remote events are fired and give the tools to the player and change the cash amount of the player and save it through the data store, that would be a more reliable system

in other words, maybe just rewrite your system while taking into account this:

that MouseButton1Click doesnt work on the server
that you gotta change the cash value on the server
that its better if you create the buttons on the local script
and that you gotta use remote events