Damage player if he's looking at a part/enemy

Hello guys, so i am currently making a Slender Man game and i need to make a script where if a player looks at the slender man, he will take damage until he stops looking at slenderman.

Is there any way i could code this kind of script? And where would i have to place the LocalScript?

And would i be able to make it so it doesn’t counts if the slenderman is behind a wall?

(Yes, slenderman has a root part so maybe it could detect if the player is looking at his rootpart or head)
Thanks in advance!

You should try raycasting.
https://devforum.roblox.com/t/detect-if-player-looks-at-part/1830346/5

1 Like

Okay, i’ll try using that. Thanks!

But in this case, where would i have to place the script? Like in what service or
object?

You could simply do something like this:

-- LocalScript
local Players = game:GetService('Players');
local RunService = game:GetService('RunService');
local ReplicatedStorage = game:GetService('ReplicatedStorage');

local Player = Players.LocalPlayer;
local Mouse = Player:GetMouse();

local damage_per_second = 1; -- Player takes this amount of damage for each second they're looking at slenderman.

RunService.Heartbeat:Connect(function(delta)
	local mouse_target = Mouse.Target;

	if (mouse_target and mouse_target.Name == 'SlenderMan') then
		print('Player is looking at the slender man!');

		local character = player.Character;
		local humanoid = character and character:FindFirstChild('Humanoid');
		if (humanoid and humanoid.Health > 0) then
			humanoid:TakeDamage(damage_per_second * delta);
		end
	end
end);

You’ll want to modify this part of the code:
if (mouse_target and mouse_target.Name == 'SlenderMan') then to be a proper reference to your slenderman object. For example, if slenderman is a model called “SlenderMan” then this code should be if (mouse_target and mouse_target.Parent and mouse_target.Parent.Name == 'SlenderMan') then

Worth noting that this is pretty easy to bypass with an exploit, but any time you rely on information like this an exploiter can just shut it off. Unfortunately there’s not really any way to know where the player is looking without the player telling you.

Ideally you’d damage the player on the server instead of on the client, but again, you’ll still have to rely on the player telling you where they’re looking, so cheaters will be able to break this system anyway.

2 Likes

Script works! Thanks.
In this case i will try to make it so instead it will damage if the part is visible on the screen, maybe surfing around the devforum.

Unfortunately yes, but i’ll try to make a anti-cheat system (i know it won’t stop cheaters forever but still)

I don’t think there’s much you’re going to be able to do about the exploiting side of things here. Even if you check if it’s “on screen” they can still lie about that. I’d just focus on making a great experience for legitimate players! :slight_smile:

1 Like

Yeah, i just want to make a fun slenderman game because he’s my favorite character and i just want to see people have fun. Thanks in advance for the script! Helps a lot.

1 Like

I’m having some problems trying to detect if the slender man is visible or not, but i’ll try to solve it.
Edit: I improved the script

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