Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How I could make a NPC sell a tool?

Asked by 4 years ago

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

~~~~~~~~DataStore Money Script~~~~~~~~~ -- 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) ~~~~~~~~~~~~~~~~~

0
You wanna sell NPCs or Tools? St_vnC 330 — 4y

1 answer

Log in to vote
1
Answered by
Time_URSS 146
4 years ago
Edited 4 years ago

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!

Ad

Answer this question