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

how could i access the player from a touched event?

i don't mean like the player model i mean the actual player in the players tab under workspace if i started from script.Parent.Touched:Connect(function(hit)

i thought it might be this but this code doesn't work, it says retracee is not a valid member of players, even though I see myself in the players..?

script.Parent.Touched:Connect(function(hit)	
	local plr = hit.Parent
	game.Players(plr.Name):Destroy()
	
	end)



then I tried this but I got lost and didn't know how to access the player who had the Id, also it seems like it can't see my userid from a touched event because the logs say userid is not a valid member of model

local players = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)	
	local plr = hit.Parent
	local player1s = players:GetChildren()
	if player1s.CharacterAppearanceId == plr.UserId then
		player1s:Destroy()
	end
	
	end)

1 answer

✓ Accepted answer

This is what you would have to do:

script.Parent.Touched:Connect(function(hit) 
	local player = game.Players:GetPlayerFromCharacter(hit.Parent) --is the part touching a player or another part?
	if player then --if it is a player that touched the part, then continue...
		 player:Destroy() --you have the player pulled out right here, now what do you want to do with this information... write your function here... do you want to destroy your player? do you want to change the player's name? All of what you are trying to do goes here.
	end
end)

This script would display the player's name in the output.

7 comments

0

that's not what im trying to do:(

1

Basically, line 2 is just getting the player from the part that touched the object the script is placed under. If it cannot get the player, that means it wasn't touching the character, it was touching something else (i.e. the baseplate). If it can get the player, then it continues to run by means of the if then function.

1

What are you trying to do?

0

under workspace in the directory, theres a category marked players, I want to know how to pick out a player if they touch a brick

1

I just did it...

0

i meant, i want to access their player in the players tab under workspace if they touch a brick

0

nevermind haha you were right i confused myself sorry

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