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

What is Wrong With This Script For Carrying Data Across Teleports?

My game is supposed to have 3 save slots so that people can retrieve their previous data upon playing again. When the player clicks a slot to load, it teleports them to the main game and depending on which slot they chose, a SetTeleportSetting function is inputted so that in the main game, a GetTeleportSetting function will be used to determine which slot the player chose and give them the appropriate data from that slot.

This is the script.

script.Parent.MouseButton1Up:connect(function() --When the player clicks this, the script activates.
	TeleportService:Teleport(561825470, player) --Teleports the player to another game.
	if saveNum.Value == 1 then
		SetTeleportSetting(player.UserId, "save1") --If the button chosen was slot 1, the value "save1" will be used and when the player enters the main game, a GetTeleportSetting function is used to retrieve that "save1" value and then determine that the player chose the 1st slot. The UserId is used as the key.
	elseif saveNum.Value == 2 then
		SetTeleportSetting(player.UserId, "save2") --Same as before, just with different slots.
	elseif saveNum.Value == 3 then
		SetTeleportSetting(player.UserId, "save3") --You get the idea.
	else
		print("Error") --Used if for some odd reason, the values are anything other than 1, 2, and 3.
	end
end)

Why doesn't this work? Can anybody identify the error in my script or give me a better method of going about what I want to accomplish?

1 answer

✓ Accepted answer

I see no problems in your code.

Here are the only problems I can think of:

  • Have you defined player?

  • I believe you have to call the teleport setting thing like so: TeleportService:SetTeleportSetting().

There are a few optimisations I could make, though.


Optimisation 1

Use MouseButton1Click, since it is designed to be clicked. MouseButton1Up is designed for when the player "unclicks" the object (and is sometimes unreliable since most ROBLOX GUI events suck).

Optimisation 2

Instead of having a bunch of if statements, you can just lower it to 1 statement.


local player = game.Players.LocalPlayer -- Is this a LocalScript?
local TeleportService = game:GetService("TeleportService")

script.Parent.MouseButton1Click:connect(function()
	local val = saveNum.Value
	if val >= 1 and val <= 3 then
		TeleportService:SetTeleportSetting(player.UserId,"save"..val)
	else
		print("Error!")
	end
	-- I'm not very experienced with TP service, but shouldn't it be teleporting after setting the TP Setting?
	TeleportService:Teleport(561825470,player)
end)

Hope I helped!

~TDP

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