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

how to make players invisible when not looking at them, and visible when looking at them ?

so uh, the title explains itself, i want to make a 2d topdown shooter, so i want to make that the player can't cheat, btw, client only.

thats my code for now

local plr = game.Players.LocalPlayer
local char = plr.Character
local Players = game.Players

for i, player in ipairs(Players:GetPlayers()) do
	if player.Name ~= plr.name then
		character = player.Character
		player.Character.Head.Transparency = 1
		player.Character["Left Arm"].Transparency = 1
		player.Character["Right Arm"].Transparency = 1
		player.Character["Left Leg"].Transparency = 1
		player.Character["Right Leg"].Transparency = 1
		player.Character.Torso.Transparency = 1
	end
end

local dot = char.Head.CFrame.LookVector:Dot(character.Head.Position)

if (dot > 0.5) then
	character.Head.Transparency = 0
	character["Left Arm"].Transparency = 0
	character["Right Arm"].Transparency = 0
	character["Left Leg"].Transparency = 0
	character["Right Leg"].Transparency = 0
	character.Torso.Transparency = 0
else
	character.Head.Transparency = 1
	character["Left Arm"].Transparency = 1
	character["Right Arm"].Transparency = 1
	character["Left Leg"].Transparency = 1
	character["Right Leg"].Transparency = 1
	character.Torso.Transparency = 1
end

i've put this script into starter character scripts, thats just a bunch of copy and paste, i hope that someone helps me

1 answer

✓ Accepted answer
local runService = game:GetService("RunService")
local plr = game.Players.LocalPlayer

local function transparency(character, value)
	for _,part in pairs(character:GetDescendants()) do
		if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" then
			part.Transparency = value
		end
		if part:IsA("Decal") then
			part.Transparency = value
		end
	end
end

local function castRay(character)
	local rayOrigin = plr.Character.HumanoidRootPart.Position
	local rayDirection = (rayOrigin-character.HumanoidRootPart.Position).Unit
	local distance = (rayOrigin - rayDirection).Magnitude

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {plr.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(rayOrigin, (rayDirection-rayOrigin).Unit*distance, raycastParams)

	if raycastResult then
		local hitPart = raycastResult.Instance
		print(hitPart)
		if hitPart.Parent == character or hitPart.Parent.Parent == character then
			return true
		end
	end
end

runService.Heartbeat:Connect(function() --fires function every frame
	for _,player in pairs(game.Players:GetPlayers()) do --goes through table of players
		local character = player.Character 
		if character and player ~= plr then --if the player's character has loaded and the player is not local player then..
			local plrToCharacter = (character.Head.Position - plr.Character.Head.Position).Unit
			local plrLook = plr.Character.Head.CFrame.LookVector
			local dotProduct = plrToCharacter:Dot(plrLook)
			if dotProduct > .5 then --if the local player is facing the character then
				local hitPlayer = castRay(character) --cast a ray to the character to see if there are any walls in between them, returns true if there aren't
				if hitPlayer then --if true then
					transparency(character, 0) --make the character opaque
				else
					transparency(character, 1) --make the character transparent
				end
			else
				transparency(character, 1) --if the player is facing away from the character then make the character transparent
			end
		end
	end
end)

I learned a lot from making this code so thanks for asking such a great question, I hope this helps :)

1 comment

0

works perfectly

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