Hey! I see what you’re trying to do, and I understand the challenge you’re facing. I’ve done something like this before
You want to create a character similar to Slenderman, where when a player looks at a specific part (the HumanoidRootPart
), the player takes damage over time. The problem seems to be with detecting when that part is added to the workspace and checking if the player is looking at it.
Here’s a better way to approach it:
Instead of constantly checking in a loop (which causes lag), you can use the Camera
’s WorldToScreenPoint
method to check if the part is within the player’s view (i.e., if it’s visible on the screen). You can then trigger the damage over time when the player is looking at the part. This is more efficient because it avoids constantly checking all parts in the workspace.
Here’s how you could approach this:
- Detect when the part is added to the workspace using
ChildAdded
.
- Use
Camera:WorldToScreenPoint()
to check if the part is visible on the player’s screen.
- Damage the player when the part is in view.
local Players = game:GetService("Players")
local Camera = game.Workspace.CurrentCamera
local function isPartVisible(part)
local screenPoint = Camera:WorldToScreenPoint(part.Position)
-- Check if the part is actually within the screen boundaries
local viewportSize = Camera.ViewportSize
return screenPoint.X >= 0 and screenPoint.X <= viewportSize.X and screenPoint.Y >= 0 and screenPoint.Y <= viewportSize.Y
end
-- (Assuming you're using a Humanoid to apply damage)
local function damagePlayer(player)
local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:TakeDamage(1)
end
end
local function onPartAdded(part)
if part:IsA("Model") and part:FindFirstChild("HumanoidRootPart") then
local rootPart = part:FindFirstChild("HumanoidRootPart")
game:GetService("RunService").Heartbeat:Connect(function()
for _, player in pairs(Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
if isPartVisible(rootPart) then -- Check if the player's character is looking at the Slenderman (part)
damagePlayer(player) -- Damage the player if the part is in their view
end
end
end
end)
end
end
game.Workspace.ChildAdded:Connect(onPartAdded)
What this script does:
- Detects when a part is added to the workspace (
ChildAdded
).
- Checks if the part is visible on the player’s screen using
Camera:WorldToScreenPoint()
.
- Damages the player if they are looking at the part (Slenderman’s
HumanoidRootPart
).
Camera:WorldToScreenPoint()
converts the part’s 3D position to a 2D screen space, which helps determine if it’s visible. BUT! If you have a custom camera script there may be a chance it could break this.
We check if the part is within the viewport boundaries to confirm if it’s visible on the screen.
The damage is applied to players looking at the Slenderman, and you can adjust the damage value as needed.
Why This Is Probably Better:
Instead of using a loop that checks every part all the time, we’re now only checking if the specific part is visible on the player’s screen. This is much more efficient and should reduce lag.
We also avoid the need for constantly checking the workspace, which was causing lag in your previous attempts.
I’m not 100% sure if this is what you are looking for, but lmk if you need more help