How would someone go about making a gear ---> gear exchange script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    So the gist is that i would like to know how to make a simple script in which exchanges a gear that the player has with another gear whenever they interact with a part.

  2. What is the issue? Include screenshots / videos if possible!
    Ive been looking around online for any sort of tutorial on this matter but all ive found are videos that show something about there being dialogue before the part gives you the item, but thats uneeded for the thing im trying to achieve

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Ive looked around the toolbox and Dev Forum and the closest thing i can find to what im trying to achieve is this wonderful little script made by Aurarus which has the “exchange” part in it but also has connections to a dialogue prompt that doesnt exist it seems.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

So basically something like this but without the included dialogue connections.

--Made by Aurarus--

----------------------------------------------------------
local nameoftoolA = "GreenestHead"	--Change "Key" to the name of the tool you want to exchange (Make sure it is in Lighting)
local nameoftoolB = "Reward"	--Change this to the name of the tool you want to award. (Must be in Lighting as well)

local responseA = "*The king of greenia rewards you*"	--This is what it'll say when the player is given the reward.
local responseB = "*The king recuires his head as proof*"
local responseC = "*The king wont reward you twice"	--This is what it'll say if the player already has the reward.

local removetool = true	--Change this to "false" if you don't want to take away the tool for the reward.
----------------------------------------------------------

function onSelect(plyr,Exchange)

if Exchange.Name == "Exchange" then
	if plyr.Backpack:findFirstChild(tostring(nameoftoolA)) == nil then

		Exchange.ResponseDialog = (tostring(responseB))

	elseif plyr.Backpack:findFirstChild(tostring(nameoftoolB)) ~= nil then

		Exchange.ResponseDialog = (tostring(responseC))

	elseif plyr.Backpack:findFirstChild(tostring(nameoftoolA)) ~= nil then

			if removetool == true then
				plyr.Backpack:findFirstChild(tostring(nameoftoolA)):remove()
			end

		local rwrd = game.Lighting:findFirstChild(nameoftoolB):clone()
		rwrd.Parent = plyr.Backpack

		Exchange.ResponseDialog = (tostring(responseA))
	

	end
end
end

script.Parent.Parent.DialogChoiceSelected:connect(onSelect)

		

I know this post is a bit of everything at once but thanks in advanced if anyone can help me out with my issue.

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You’re being somewhat vague and I’m struggling to understand what you’re asking.

When you say gear, do you mean ROBLOX gear like the ones you would buy from the catalogue? Or do you mean your own system for tools in your game, or do you mean standard roblox tools.
And “interact with a part”, what does this mean, when their character touches it, a click detector or proximity prompt?

Seems like you need to break this problem down a bit more, then search for help on those smaller problems.

2 Likes

Huge apologies for being a bit vauge with my question beforehand, so yes i do mean gear as in roblox tools and interact as in when the player uses a proximity prompt. (so i.e if a player has a sword, they will press e on a part and it will remove the sword from the players inventory and then give them a different tool in return)

“(so i.e if a player has a sword, they will press e on a part and it will remove the sword from the players inventory and then give them a different tool in return)”

Seems to me like you know the logic required! I will mention a few things though.

So ROBLOX tools (thats this class: Tool | Documentation - Roblox Creator Hub),
move about the hierarchy when a player equips or unequips them (as well as dropping them).
This is an issue as when we call the function to “exchange” a tool, the main logic I presume will follow something like:

  • Find if the player already has a tool
  • If they do, destroy it
  • Give them the new tool

However, since the tool will move about the hierarchy, actually finding out whether the player has a tool already can be a bit more challenging.

You might already know this but I’ll explain anyway.
When a user is playing a game, there are 2 objects associated with the user, a Player and a Character.
The Player belongs to the Players Service, whereas the Character belongs to the workspace. Whenever you die or reset, the Character object gets destroyed and remade, the Character object is your physical model. However, the Player object will only be removed when you actually quit the game.

So what does this mean when we’re on about this gear exchange system?
ROBLOX tools will switch between belonging to the player and the character as they’re equipped and unequipped.

Belonging to the Player object is “Backpack”
image
Which stores all the tools that the player has, but aren’t equipped.

See here:
My toolbar:


My Backpack:
image

If it was just like this, finding out whether the player already had a tool would be easy, as we would just search the players backpack for a tool, and if they do have one, delete it and add a new tool.
However, look what happens when I equip the sword tool.


image
Its disappeared from my backpack and now belongs to my character model
image

Okay, using that information I’ll now produce a demo of the gear exchange system you’ve outlined, and I will show my steps.

Firstly lets break down the problem:

  • We need a way to interact with a part using a proximity prompt
  • We need an “exchangeGear” function
  • This function needs to be able to find if the player already has a tool and delete it
  • This function needs to be able to add a new tool to the players backpack

First step is rather simple:
image

Now lets make it so we can detect when that prompt is triggered.
This script is just a child of the part.
image
image
You can see that the “player” parameter of the function is equal to the Users Player instance and not its character instance.

I’m going to make it so that the part adds the tool to the players backpack first, rather than exchanging it for now.
Storing all the tools in server storage, so we can clone them into the backpack:
image
image
Pretty simple so far.

However, this does mean we can give ourselves loads of tools, as they currently aren’t being exchanged.

So lets work out the logic for exchanging a tool:

  • We need to find the players current tool
  • If they have one destroy it
  • Then add the new tool

However, like we said earlier, finding the current tool is made more difficult by the way it switches about the hierarchy when equipped.
So its more like:

  • Does the player have a tool belonging to their character model?
  • If so destroy it
  • Destroy all the contents of the player backpack
  • Add the new tool

And it works.
Heres the place file: GearExchangeDemo.rbxl (56.5 KB)

Any questions just ask, and if I misunderstood your query just tell me.

2 Likes

Oh cheers dude, not only did you help me out with my little problem, you’ve also made me have a little bit better of an understanding of how tools work in the workspace, i now see what I probably should of done instead when attempting to make this script yesterday and ill make sure if i have any more queries i will be sure to message. But for now thanks a million

2 Likes

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