Hey Everyone,
I am making a role system similar to break in story i almost got it working but the only problem is I dont know much about datastores so when the player gets their tool, medkit for example it does not carry onto the main game after the teleport. Thanks
my bad for the audio btw i forgot to turn the mic off
For this case, I would advice you don’t use DataStoreService. Rather, you can use the TeleportService
itself. See, when you teleport between 2 games, you have the option to send information from the original game to the new game.
Therefore, all you need to do is send the role that the player chose in the Lobby to the main game.
You can do this using the TeleportOptions instance, and calling the :SetTeleportData()
function.
For example:
local data = {
["Role"] = "Adult",
["Class"] = "Medic"
}
local TeleportOptions = Instance.new("TeleportOptions")
TeleportOptions:SetTeleportData(data)
Then, on the main game, you’d use the PlayerAdded
function in order to retrieve this data.
game.Players.PlayerAdded:Connect(function(player)
local joinData = plr:GetJoinData()
local teleportData = joinData.TeleportData
print(teleportData.Role) -- would print "Adult"
end)
i never thought about it like that let me try that rn
Of course. I’ve edited my reply to include an example, have a look.
i was able to edit my lobby script, not finished with the main game script but here is the lobby one
-- Function to determine the player's role based on inventory
local function determinePlayerRole(player)
-- Check if the player has the medkit tool in their inventory
local medkit = game.ReplicatedStorage:FindFirstChild("Medkit")
if medkit and (player.Backpack:FindFirstChild("Medkit") or player.Character:FindFirstChild("Medkit")) then
print("Medic assigned to player")
return "Medic" -- Player has the medkit, assign the "Medic" role
else
return "Kid" -- Player does not have the medkit, default to "Kid" role
end
end
-- Connect an event to handle role assignment and teleportation
game.Players.PlayerAdded:Connect(function(player)
local roleName = determinePlayerRole(player)
-- If the role is "Medic," give the player the medkit tool in the lobby
if roleName == "Medic" then
local medkitTool = game.ReplicatedStorage:FindFirstChild("Medkit") -- Replace with the actual medkit tool
if medkitTool then
medkitTool:Clone().Parent = player.Backpack
else
warn("Medkit tool not found in ReplicatedStorage.")
end
end
-- Use TeleportOptions to send role information to the main game
local teleportService = game:GetService("TeleportService")
local placeId = 1234567890 -- Replace with the main game's place ID
local teleportOptions = Instance.new("TeleportOptions")
local teleportData = {
Role = roleName -- Pass the role information
}
teleportOptions:SetTeleportData(teleportData)
local success, errorMessage = pcall(function()
teleportService:Teleport(placeId, player, teleportOptions)
end)
if not success then
warn("Teleportation error: " .. errorMessage)
end
end)
the problem im having now though is that i dont want it to teleport the player i just want it to give the role info
I thought you said you needed the role info after the teleport was done?
yeah but there is a problem even with the edited script it doesnt carry over to the main game for some reason.
Did you give them the item in the main game? Once the player is teleported, you need to have another script in that game that gives them the item after they’ve joined.
yeah once the player is teleported the script is supposed to check their role
Yeah. I gave you an example of how to do it in my text above. Have you been able to accomplish this?