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

How do I check for while touching instead of onTouch?

Hey scriptinghelpers, so I don't actually know how to do this. I can't seem to find much about it either. I was wondering how do I check for a player (in my case probably multiple players but I want it all to be separate since it's awarding points) while they're touching a part instead of onTouch. The onTouch is a one time deal, I'm looking to wait 1 second then continue to award 1 point every second while they player is touching the part. Below you find my onTouch code that I'm looking to turn into a while touching. I'm not sure how to do this.

This is a Script inside of a Part (inside of Workspace)

local part = script.Parent
local function onTouch(tixPart)
	local humanoid = tixPart.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local player = game.Players:FindFirstChild(tixPart.Parent.Name)
		if player then
			player.leaderstats.Tix.Value = player.leaderstats.Tix.Value + 1
			wait(1)
		end
	end
end
part.Touched:Connect(onTouch)

I do know about TouchEnded but, I don't really know like the way I would go about checking for "while touching". Like I said there will be multiple players inside the part at one time so, I do want to make sure that all the players are awarded the points while they're touching the part. Not just one player. I did try it with this, but it's a one time deal unless you retouch it again. I actually removed the debounce because I thought it might help but, now it just awards multiple for every player part (head, arms, torso, etc.) that touches it. Once again, this is only a one time deal. Thank you for the help in advance! I appreciate it.

1 answer

✓ Accepted answer
local Players=game:GetService"Players"
local function getPlayersOnPart(regionPart,regionHeight)
	local pos1 = regionPart.Position+Vector3.new(0,.3,0) - (regionPart.Size / 2)
	local pos2 = regionPart.Position+Vector3.new(0,.3+(regionHeight or 0),0) + (regionPart.Size / 2)
	local region = Region3.new(pos1, pos2)
	local partsInRegion = workspace:FindPartsInRegion3(region, nil, 1000)
	local tab,x={},0
	for _, v in ipairs(partsInRegion) do
		local char = v.Parent
		local hum = char and char:FindFirstChildOfClass"Humanoid"
		local player=hum and Players:GetPlayerFromCharacter(char)
		if player and not tab[player]then
			tab[player]=true
			x+=1
		end
	end
	for _,v in ipairs(Players:GetPlayers())do
		tab[v]=not not tab[v]
	end
	return tab,x
end
local function award(player)
player.leaderstats.Tix.Value+=1
end
player
while wait(1)do
	local players,num=getPlayersOnPart(workspace.Baseplate) -- choose part
	--print("number of the players on a part: "..num)
	for player,isOnPart in pairs(players)do

if isOnPart then
-- code
pcall(award,player)
end




	--	print(player,isOnPart)
	end
end

2 comments

0

Oh sweet, thank you scripterhelper5354. I did some testing and there is an error on line 25 but I just removed it and it works perfectly fine. I'll get back to you after I finish testing some more. Thanks again! I really appreciate it.

0

That's pretty awesome. I never even knew about this! Thanks again again, I learned something new today.

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