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

Why does this function stop firing after this if statement?

I'm having problems with this function. I'm not getting any errors here. I've tried putting the if statements into separate functions and connecting the functions on separate lines. For whatever reason, the entire function seems to stop detecting the touched event from the humanoid after the RoomTeleport statement. Here's the code:

local Character = script.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local debounce = false
local Falling = nil

Character.Humanoid.Touched:Connect(function(hit)
	if not debounce then
		if hit.Name == "TeleportIslandPart" or hit.Name == "FallingTeleport" then
			debounce = true
			if hit.Name == "FallingTeleport" then
				Falling = true
			else
				Falling = false
			end
			game.ReplicatedStorage.Teleport:FireClient(Player, Falling)
			wait(3)
			debounce = false
		return end
		if hit.Name == "RoomTeleport" then --the function stops working after this statement is done
			debounce = true
			local RoomName = hit.RoomName.Value
			game.ReplicatedStorage.CreateRoom:FireClient(Player, RoomName)
			wait(3)
			debounce = false
		return end
		if hit.Name == "RoomLeave" then	
			debounce = true
			local RoomName = hit.RoomName.Value
			local Island = hit.Island.Value
			game.ReplicatedStorage.LeaveRoom:FireClient(Player, RoomName, Island)
			wait(3)
			debounce = false
		return end
	end
end)

Here is the CreateRoom code:

local Player = game:GetService("Players").LocalPlayer
local Teleporting = false

game.ReplicatedStorage.CreateRoom.OnClientEvent:Connect(function(RoomName)
	if not Teleporting then
		Teleporting = true
		local Room = game.ReplicatedStorage.Rooms[RoomName]
		local Character = Player.Character
		game.ReplicatedStorage.RequestAddLoadScreen:FireServer(RoomName, false, false, true)
		wait(2)
		Room.Parent = workspace
		Player.Backpack.CurrentMusic.Value = Player.Backpack.MusicFolder[RoomName.."Music"]
		Character.HumanoidRootPart.CFrame = workspace.TeleParts[RoomName.."TelePart"].CFrame
		game.ReplicatedStorage.RequestRemoveLoadScreen:FireServer()
		Teleporting = false
	end
end)

7 comments

0

So the room teleport function works fully and then the script stops or the room teleport doesn't print anything?

0

that's right

0

Oh sry, i mistyped the last debounce as "true" again instead of false, try that, otherwise i'll look at it again in a bit

0

still not working. getting the same results

0

hmm, so does the "roomleave" work appropriately? The below should work, so it may be something with the client side local script, can you paste the portion pertaining to the CreateRoom remote onto your post?

0

LeaveRoom should be working properly, but it's just not firing. i've added the code for CreateRoom to the post

0

can you send a tc so i can have a look directly? since these seem to be fine, although i would use waitforchild / getservice in the createroom script, though if you dont have errors for it then idk where the issue is

1 answer

Improvements

Use GetService() for the Players and ReplicatedStorage Services

Use elseif instead of multiple if then statements when they are all checking the same item

The debounce can be moved outside of the if then since they all require it.

Issues

By using hit.RoomName and hit.Island you are automatically assuming that any part that touches the player will have those instances under them. It will stop functioning if you touch an object that doesnt contain the instances. Using FindFirstChild() can help you check for them

Revised ServerScript

local Character = script.Parent
local Player = game:GetService("Players"):GetPlayerFromCharacter(Character)

local repst = game:GetService("ReplicatedStorage")
local tremote = repst:WaitForChild("Teleport")
local cremote = repst:WaitForChild("CreateRoom")
local lremote = repst:WaitForChild("LeaveRoom")

local debounce = false
local Falling = nil

Character:WaitForChild("Humanoid").Touched:Connect(function(hit)
	if debounce == false and (hit.Name == "TeleportIslandPart" or hit.Name == "FallingTeleport" or hit.Name == "RoomTeleport" or hit.Name == "RoomLeave") then
		local hr = hit:FindFirstChild("RoomName")
		local hi = hit:FindFirstChild("Island")
		debounce = true
		
		if hit.Name == "TeleportIslandPart" then
			Falling = false
			tremote:FireClient(Player, Falling)
		elseif hit.Name == "FallingTeleport" then
			Falling = true
			tremote:FireClient(Player, Falling)
		elseif hit.Name == "RoomTeleport" and hr then
			local RoomName = hr.Value
			cremote:FireClient(Player, RoomName)
		elseif hit.Name == "RoomLeave" and hr and hi then 
			local RoomName = hr.Value
			local Island = hi.Value
			lremote:FireClient(Player, RoomName, Island)
		end
		wait(3)
		debounce = false
	end
end)

1 comment

0

this still isn't working. i made it so that it'll print something every time the humanoid is touched. after the RoomTeleport, it completely stops printing. also, humanoids have an event called touched, which will fire if any part of the character is touched

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