Can you save an equipped tool between places?

Hello fellow devs! So I am making a dungeon game similar to Soda Dungeon. Currently in the game I have a functional inventory system that I am using in the lobby. However, what I am trying to accomplish is the following: I would like to save the equipped tool in some way so the tool the player equips in the lobby remains equipped when they teleport to the dungeon. This way when they play it will still be equipped when they enter the dungeon game. Any help is appreciated. Any links of articles or tutorials regarding how I would accomplish this would also be appreciated so I can learn. Thanks in advance!
Edit: Fixed Grammar

2 Likes

You will need to use a datastore, of course. But, you can transfer data between places. But, you have to create the place under your main game, which you must do in studio. You can then call the same datastore in that place, and the data is there in both games. If you haven’t made a datastore before, take a look at this devhub page. Or, you can look on youtube tutorials if you learn better that way.

2 Likes

Ok. I don’t know why I didn’t think of a data store, I must’ve been over thinking it. Do you think I could use a string value to hold which tool the player equips that gets saved by my data store. Then when the player enters the dungeon I could loop through the players backpack to check if a tool matches that saved value, then equip that tool if it does? That’s just an idea I came up with just now a few minutes before writing this. I think this could work however I wouldn’t mind any criticism on the idea, it just helps me get better.

Unless you are also saving the tools in a player’s backpack, that way won’t necessarily work, or at least how you explained it. Saving a string value is definitely a viable way to do this, but the execution will have to be different. Since the player isn’t coming into the teleported place with anything in their backpack, you could instead go through the names of tools that you have stored in serverstorage(just some place to store them) and then if the string value matches the name of a tool, you could clone the tool to the backpack, or if you want it equipped right away, I think cloning it straight to the character would work.

Yeah that could actually work quite well. I’ll play around with it to see if I can get it to work and get back to you if it works. I’ll mark as solution if it ends up working out, otherwise I’ll let you know.

TeleportService:SetTeleportSetting(Tool.Name, false) -- example

TeleportService is an excellent ally for you in this case.

@DarkDanny04 There is no need to use DataStores for this, see the above, it’s much better for this use case.

EDITS: Tagged @DarkDanny04 and fixed the function name.

1 Like

Ah, I hadn’t known about this until now. You will most likely want to use this way then.

Nevertheless, thank you for educating me on this topic. I very much appreciate it!

Yeah I am with @DarkDanny04, I hadn’t heard of that function until now either. The only thing I am a little confused on is the parameters. Would the first parameter, which is the string value, be like a string value of the of the tool the player equips that I use the value for the parameter? Also for the value I want to store, would I have to get the sword from replicated storage if I’m holding it there so then I can store the value? It would help a lot because from what I can see it is a very useful tool I can see myself using more often in the future.
Edit: My last question would have to be - When using this do I only need to set the values in the original game? Or do I need to update it every time I teleport the player? I’m just curious because it’s my first time using this so I don’t wanna mess it up.

Okay, so I’ve read up on the teleport service more, and I found this devhub page that can help do what you want it to do. It isn’t what that other person said, but it’s along the lines of that

1 Like
game:GetService("TeleportService"):SetTeleportSetting("EquippedTool", 
 "tool1"  -- this can be defined through your own mechanism, the player's equipped tool
)

-- Teleport the player

local tool = game:GetService("TeleportService"):GetTeleportSetting("EquippedTool")
if table.find(AllValidTools, tool) then print("you were equipping tool: ".. tool) end
-- validate tool, exploiters could abuse the SetTeleportSetting method's parameters (on the server, I just did it here for demonstration)

Validate stuff on the server as well if you have to.

Edit: TeleportData looks better for the purpose, though probably essentially the same thing

3 Likes

Thanks for making the reply, I understand how using TeleportService:SetTeleportSetting() works a bit better especially after looking at the api reference for TeleportService:GetTeleportSetting() and the code you posted. Although you and @bt5191 both used this method I will mark you as solution because you were most recent. Once again thanks for the reply and thanks to everyone else who replied, I learned some valuable functions under TeleportService.
Edit: After looking at the TeleportData link I will probably use that method because I can store the equipped item as an object value that I send as the data with the player.