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

While is touching function? Help!

Hello guys, I'm having a hard time figuring out how to do a IsTouching function for my script to work, what I want to do is that if the player is in a determined area, can press a button which will animate the player through a parkour vault and then move to the other side of the brick, everything works but not very well. I tried with Touched and TouchEnded, but each ticks they are both called and this result in the script to work only sometimes.

This is the current code:

brick.Touched:connect(function(otherPart)
	local char = otherPart.Parent
	local hum = char:FindFirstChild("Humanoid")
	if hum then
		local player = game:GetService("Players"):GetPlayerFromCharacter(char)
		local flags = player:FindFirstChild("Flags")			
		if flags:FindFirstChild("CanKong").Value == false then
			flags:FindFirstChild("CanKong").Value = true
			flags:FindFirstChild("KongInstance").Value = script.Parent.EndPoint
			print("Can kong");
		end
	end
end)

brick.TouchEnded:connect(function(otherPart)
	local char = otherPart.Parent
	local hum = char:FindFirstChild("Humanoid")
	if hum then
		local player = game:GetService("Players"):GetPlayerFromCharacter(char)
		local flags = player:FindFirstChild("Flags")			
		if flags:FindFirstChild("CanKong").Value == true then
			flags:FindFirstChild("CanKong").Value = false
			flags:FindFirstChild("KongInstance").Value = nil
			print("Cannot kong");
		end
	end
end)

I want it to work every time I press the key, and not sometimes!

2 comments

0

try remove endpoint

0

the endpoint is not related to the touched event, it's just the brick where the animation should finish...

1 answer

Use a larger invisible part that covers the area around the brick and use it as a detector.

I was talking about something like this:

https://imgur.com/a/jE6OY

Also, here is a code that creates an event that fires only when the entire character entered or exited the area:

local T = {}
local EvObj = Instance.new("BindableEvent")
script.Parent.Touched:Connect(function(p)
	local Ply = game.Players:GetPlayerFromCharacter(p.Parent)
	if Ply then
		if T[Ply.Name] then
			T[Ply.Name][p.Name]=true
		else
			T[Ply.Name]={[p.Name]=true}
			EvObj:Fire(true,Ply)
		end
	end
end)
script.Parent.TouchEnded:Connect(function(p)
	local Ply = game.Players:GetPlayerFromCharacter(p.Parent)
	if Ply then
		if T[Ply.Name] then
			T[Ply.Name][p.Name]=nil
			if #T[Ply.Name]==0 then
				EvObj:Fire(false,Ply)
				T[Ply.Name]=nil
			end
		end
	end
end)

local TouchEvent = EvObj.Event
TouchEvent:Connect(function(e,p)
	print(e,p)
end)

3 comments

0

I did it like this, the problem is that TouchEnded calls at every tick and so my function returns true

1

I'm gonna check when I come home

0

I edited my post

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