Why doesnt this code work?

i wanted to make a code that makes it so when the player touches a part then it will load in a map which is in a folder and it will make a ui apear then after the maps done loading it would teleport the player to a part

for the teleporting i have a part named TPPart

for the ui i have a screengui called Loading and a textlabel named TextLabel

for the folder its in serverstorage and its named level1

btw the script is parented to the part that you touch

-- local Players = game:GetService("Players")
local SS = game:GetService("ServerStorage")

local Part = script.Parent

local debounce = {}

local function OnPartTouched(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)

	if not player then return end
	if debounce[player.UserId] then return end

	local map = SS.level1.MapName:Clone()
	map.Parent = game.Workspace
	debounce[player.UserId] = true

	local PlrGui = player.PlayerGui
	local ScreenGui = PlrGui.Loading
	local Label = ScreenGui.TextLabel

	task.spawn(function()
		Label.Visible = true
		task.wait(1)
		debounce[player.UserId] = nil
		task.wait(4)
		Label.Visible = false
		local char = player.Character
		char.HumanoidRootPart.Position = map.TPPart.Position
	end)
end

Part.Touched:Connect(OnPartTouched)

I would personally use RemoteEvents to handle things between the server and client, like UI. Stuff like teleporting and spawning the map should be handled on the server. Hope this helped!

What is the script not doing right exactly? is it loading the map but not the ui? just not teleporting? etc

the only problems I see are not fatal problems that would stop the script from doing anything

but all I can see is that the map is going to get cloned multiple times so your going to end up with 1000s of clones in the workspace

and another small thing is your using task.spawn for no reason

this is how i would write your script

local map = nil
local debounce = {}

script.Parent.Touched:Connect(function(part)
	if part.Name ~= "HumanoidRootPart" then return end
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if player == nil then return end
	if debounce[player] ~= nil then return end
	debounce[player] = true
	player.PlayerGui.Loading.TextLabel.Visible = true
	if map == nil then
		map = SS.level1.MapName:Clone()
		map.Parent = game.Workspace
	end
	task.wait(5)
	player.Character:PivotTo(map.TPPart.CFrame)
	player.PlayerGui.Loading.TextLabel.Visible = false
	debounce[player] = nil
end)

i dont think its doing anything

Can you place print statements throughout to see where exactly its getting stuck?

wdym by print statements i dont know how to code lol
someone else made this code for me

Well a print statement is basically just a little line saying whatever text you put in or any data you tell it to say or well “print” into the output like so

print("Hello World!")

usually its a good idea to put them in different spots throughout scripts that are having issues so you can determine where its failing like so:

--code code code
print("Passed if statement")
--code code code code code
print("Passed for loop for players")

What you could do is add a Folder in replicated storage called “Maps” with all your maps inside of it (Make sure each map is a model) (Make sure the Map is named “Map”)

After that add a script In the Part and also Add a Remote event named “ChangeMap” in ReplicatedStorage.

Make sure to add a Part inside of all the Maps called TPPart

Server Script

local Part = script.Parent

Part.Touched:Connect(function(Hit)
         local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
         if Player then
           game.ReplicatedStorage.ChangeMap(Player)
      end
end)

Local Script



game.ReplicatedStorage.ChangedMap.OnClientEvent:Connect(function(Player)
      game.Workspace.Map:Destroy()
      local NewMap = game.ReplicatedStorage.Maps.YourPart:Clone()
      NewMap.Parent = game.Workspace
     Player.Character.HumanoidRootPart.Position = Map.TPPart.Position
end)
1 Like

ill try this when i have time (im working on a game with someone :slight_smile: )
also i made a folder for each map there not a model

OK so the character is a model with the primary part HumanoidRootPart. To set the characters position somewhere else do char:PivotTo or char:SetPrimaryPartCFrame

The latter is deprecated
Pass a CFrame as an argument

That is the best methods for me anyway
HOWEVER
the character has many motor6ds so all the parts are connected
To update all relative positions u need to set the CFrame of the HumanoidRootPart
And only the HumanoidRootPart bc I think hat all motor6ds end up there

If you do t wanna read the above then just type char.HumanoidRootPart.CFrame = TPPart. CFrame

If u don’t know cframes go check them out
I typed all of this from phone so there might be some autocorrect and typos