How do I detect when a player touches a brick, but only certain things happen to the player who touched it? (read desc)

I’m making a game where when the player enters the house, their lighting effects change, but for people outside the house, everything is normal. To detect if a player is inside, I want the game to check if they are touching a certain brick.
How would I do this so that ONLY the players touching the brick have the lighting effects applied to their screen?

5 Likes

You would run the event:

workspace.part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
--do stuff locally, so only for 1 player
end
end)

but in a local script, and in staterGui or starterPlayerScripts because local scripts don’t run in workspace.

5 Likes

this actually helps me with my Marble run game, however, do you know how to make it so it checks for local parts, and it also works with multiple parts, in a folder

2 Likes

thank you for that!
Do you know how to make it so it only does it when the player is touching the brick, otherwise, it will stop?
like using an if else statement or something like that?
So like if player is touching the brick, run all the scripts. Else, stop the scripts. Somehting like that?

2 Likes

since it’s a local script, it’s only going to check parts locally. In folders, you would simply add this to the event:

for i,v in pairs (folder:GetChildren()) do
v.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
--do stuff locally, so only for 1 player
end
end)
3 Likes

same thing as before, but add Magnitude to it.

local db = false

workspace.part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if db == true then return end
db = true
--do stuff locally, so only for 1 player
local magnitude = (hit.Position - workspace.part.Position).Magnitude
repeat
wait(1)
--do more stuff, infinitively until player stops touching
local magnitude = (hit.Position - workspace.part.Position).Magnitude
until magnitude > --[[part size of x/z axis divided by 2, or any distance so that it stops--]]
db = false
end
end)

for reference: Vector3 | Documentation - Roblox Creator Hub

3 Likes

GOT IT!
Thank you so much!

3 0 c h a r a c h t e r res dwfha 'ogf

2 Likes

The only issue I have is that everything stops after like 1 seconds, because it thinks that the player is no longer touching the brick.

2 Likes

you can increase the number compared in

until magnitude > 

to something bigger

2 Likes

Very simple, if your referring the sky, blues and other stuffs relating to lighting, add those in camera or workspace.CurrentCamera. This will only take effect of locally and not server. If you place these in the lighting property, everyone will see the same changes.

1 Like

There’s an issue with this code. If a random humanoid model (NPC) touches the part, this would result in an error. I recommend using Players:GetPlayerFromCharacter(part) to collect the player.

For example:

local player = game:GetService'Players':GetPlayerFromCharacter(touchedpart.Parent)

if player then
   RemoteEvent:FireClient(player) -- or RemoteFunction:InvokeClient(player) if there's an OnClientInvoke registered for this remote function in a local script
end
2 Likes