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

The 'Part.Touched' Script i made doesnt always run. Sometimes it doesnt and sometimes it does?

My touched script doesnt always run for some reason. And i dont know why. Here's my script

local door = script.Parent
local donut = workspace.DonutRoom.DonutTeleport
local pizza = workspace.PizzaRoom.PizzaTeleport
local apple = workspace.MineshaftRoom.MineshaftTeleport
local billysays = game:GetService("ReplicatedStorage"):FindFirstChild("BillySays")

door.Touched:Connect(function(hit)
	script.Disabled = true
	if hit.Parent:FindFirstChild("Humanoid") then
		local char = hit.Parent
		for i = 1,1 do
			local rndm = math.random(1,3)
			if rndm == 1 then
				char.HumanoidRootPart.Position = donut.Position
				billysays.Value = "I want that DONUT!"
			else
				if rndm == 2 then
					char.HumanoidRootPart.Position = pizza.Position
					billysays.Value = "GET ME THE PIZZA! I WANT THE PIZZA!"
				else
					if rndm == 3 then
						char.HumanoidRootPart.Position = apple.Position
						billysays.Value = "I WANT THE APPLE! APPLE APPLE APPLE!"
					end
				end
			end
		end
	end
end)

It runs sometimes. and sometimes it doesnt. Is this a studio glitch or a scripting error?

2 answers

✓ Accepted answer

The CFrame.new is the position in the map you want a player to teleport to. Simply get a brick and put it where the player is supposed to teleport, check the brick's position, copy the 3 numbers, delete the brick and put the numbers inside of CFrame.new(...). Hope this helped!

function ontouch(hit)

	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

	if plr then

			hit.Parent.HumanoidRootPart.CFrame = CFrame.new(0,0,0)

		end
	end

script.Parent.Touched:Connect(ontouch)

2 comments

0

Thank you!

0

Thank you!

If what you mean is that you only want it to happen ONCE then this is your solution:

your script:

    script.Disabled = true

is inside of the hit function but will disable even when there is no HUMANOID FOUND.

to fix this simply move the script.Disabled = true under your if statement like so:

door.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
     script.Disabled = true

2 comments

0

I want it to run more then once. I want everytime i touch it so it teleports me to one of the parkour rooms. But it doesnt always work

0

oH. I realised what i did wrong. Im marking this as true because it helped me realise that i made that error

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