Script only runs while a player is standing on a specific brick

Hi! I’m pretty sure what I am trying to do is supposed to be simple, however I cannot figure out how to script it. What I’m trying to do is have a simple brick turn visible when it detects that a player is standing on it. As soon as the brick doesn’t detect a player standing on it, the brick will turn invisible. How would I be able to achieve this?

1 Like

You would use .Touched and .TouchEnded to detect when the brick is touched and when it’s not. In those functions, you can set Transparency to 1 and 0 respectively.

1 Like

In this case, you would want to connect to Humanoid.Touched to avoid too many connections.

--StarterPlayerScripts
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local Player = Players.LocalPlayer
local RenderStepped = RunService.RenderStepped

local rcParams = RaycastParams.new()
rcParams.FilterType = Enum.RaycastFilterType.Include

local down = -Vector3.yAxis
local invisibleTransparency = 0.6 --change to 1 to make it fully invisible

function onCharacterAdded(character)
	if not character then
		return
	end
	local inProcess = false
	local Humanoid = character:WaitForChild("Humanoid")
	local Root = Humanoid.RootPart
	Humanoid.Touched:Connect(function(hit)
		if inProcess then
			return
		end
		--If the hit part exists and is named 'transparentBrick', initialize the raycast params and make it visible.
		if hit and hit.Name == "transparentBrick" then
			inProcess = true
			rcParams.FilterDescendantsInstances = {hit}
			hit.Transparency = 0
			
			--[[
			Every frame, check if the raycast result is returned. If not, the part is
			not on the humanoid and we can end the loop and make it invisible.
			]]
			while true do
				local result = workspace:Raycast(Root.Position, down * 3, rcParams)
				if not result then
					break
				end
				RenderStepped:Wait()
			end
			hit.Transparency = invisibleTransparency
			inProcess = false
		end
	end)
end

onCharacterAdded(Player.Character)
Player.CharacterAdded:Connect(onCharacterAdded)

In the code above, the part that the humanoid steps on is checked via raycasting downwards and as soon as the humanoid exits the position, the part is made invisible.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.