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

How can I use WorldToViewportPoint In order to damage the player (?)

I want to make a part that damage you when you look at It, so I am deciding to use WorldToViewportPoint In order to make It, however the problem Is that I am new to It, can anyone help me?

1 answer

✓ Accepted answer
local LocalPlayer = game:GetService("Players").LocalPlayer
local PartYouWantToLook = workspace.Part
local Debounce = false
local function isPointVisible(Part)
	local camera = workspace.CurrentCamera
	local WorldPoint = Part.Position
	local vector, onScreen = camera:WorldToViewportPoint(WorldPoint)

	if onScreen then
		local origin = camera.CFrame.Position
		local ray = Ray.new(origin, WorldPoint - origin)
		local Params = RaycastParams.new()
		Params.FilterDescendantsInstances = {Part,LocalPlayer.Character}
		local Hit = workspace:Raycast(ray.Origin,ray.Direction,Params)
		if Hit then
			return false
		end
	else
		return false
	end
	return true
end

while task.wait() do
	if isPointVisible(PartYouWantToLook) and not Debounce then
		Debounce = true
		LocalPlayer.Character.Humanoid:TakeDamage(20)
		task.wait(1)
		Debounce = false
	end
end

2 comments

0

It works! thanks! Also quick question, how can I make It so It damages the player when looking at a part Inside of a model, because apparently, when a player looks at a torso of a NPC, It doesn't damage them

0

see the ignore RaycastParams add your torso or anything you think its blocking it another thing to note is that this is a local script and you must or need to add a remote event so the server actually knows you died

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