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 make an onTouch command only visible for the player who touched the part first?

So I have an onTouch Script and if someone touches it the event will happen for all users. How do I make it so that only the player who touched the part with the onTouch script can see the event. The script I use for onTouch Events:

local function onTouch(hit)
[Code goes here]
end

script.Parent.Touched:Connect(onTouch)

2 comments

0

Please provide more information about what you wanted to do after the player touches the part

0

I want to destroy something but that it only destroys for the player who touched the Part with the Script but not for the other players

1 answer

✓ Accepted answer

Edit: Referring to your comment, you forgot to put an "end" to close the function and you would also have to check if it's a player

function onTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid)" then
	--code here
	end
end

script.Parent.Touched:Connect(onTouch)

If you mean all players (game.Players) then

function onTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then --checks if it's a real player
		for i,v in pairs(game.Players:GetPlayers()) do --goes through the list of game.Players
		--code here (use v for the players in game.Players) 
		end
	end
end
	
script.Parent.Touched:Connect(onTouch)

But if you mean all of the character's players then

function onTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then --checks if it's a real player
		for i,v in pairs(game.Players:GetPlayers()) do --goes through the list of game.Players
		v.Character -- add code here
		--example: v.Character.Humanoid.Health = 0
		end
	end
end

script.Parent.Touched:Connect(onTouch)

3 comments

0

I actually mean that it affects all player but I want that it only affects the player who touched the part with the onTouch Script.

0

Alright I edited my answer

0

I only forgot to put it in the title but I always add end after the script so I edited it.

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