Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How I could make a NPC sell a tool?

Hello, I was wondering how I could make a NPC sell a tool when you click its torso. So far I have a script for DataStore / Money leaderboard.

------Here is what my Game Explorer Looks like https://gyazo.com/488bdcd1f297e1beb22d9c072f8c2458

-- How to save data in your Roblox game

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	cash.Value = 450
	
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserID.."-cash")
	end)
	
	if success then
		cash.Value = data
	else
		print("There was an error whilest getting your data")
		wait(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
	myDataStore:SetAsync(player.UserID.."-cash",player.leaderstats.Cash.value)
	end)
	
	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
	
end)

1 comment

0

You wanna sell NPCs or Tools?

1 answer

Insert a ClickDetector and a Script inside the NPC's Torso.

Making a GUI Shop

Insert the GUI of the shop you should have made in somewhere you want to. I've chosen the same Torso. Instead of each button that represents each item, insert an IntValue, name it Price, and set the price of the item as Value.

Before you start with the script, I recommend moving all the tools to the ServerStorage as Lighting is no longer used for storing. This script stores all the items to be sold inside a folder called "ShopTools" inside ServerStorage.

Now, inside the script, write this:

local Torso = script.Parent -- The script is inside the Torso.

local Tools = game:GetService("ServerStorage"):WaitForChild("ShopTools") -- Change this
local ClickDetector = Torso:WaitForChild("ClickDetector")

local GUI = Torso:WaitForChild("ShopGUI") -- Change this

ClickDetector.MouseClick:Connect(function(Player) -- ClickDetectors can detect the player who triggered the event.
	local ClonedGUI = GUI:Clone()
	ClonedGUI.Parent = Player:WaitForChild("PlayerGui")
end)

Scripting the Shop GUI

There are several ways to script a GUI. In this case, I suggest you watch this video explaining how to make a basic Shop GUI: How to make a GUI Shop. This video explains very well how to make a FE Shop GUI using Remotes, and how to work with Data Storing and Currencies.

Hope this helped enough for you to start!

This discussion is preserved as it was. New answers, comments, and votes are closed.