Use Player.Character.Head and Player:GetMouse() function with a little bit touch of CFraming.
local script in starterCharacterScript
local character = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
if character or mouse == nil then return end
mouse.Move:Connect(function()
remote:FireServer(mouse.Hit.Position)
end)
Script in serverScriptService
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local visualize = false -- change to true to see ray.
remote.onServerEvent:Connect(function(player, mouse)
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local head = character:WaitForChild("Head")
if humanoid.Health > 0 then
local origin = head.Position
local direction = (mouse - origin).Unit*300
local result = game.Workspace:Raycast(origin, direction)
local intersection = result and result.Position or origin + direction
local distance = (origin - intersection).Magnitude
if visualize == true then
local g = Instance.new("Part")
g.Size = Vector3.new(0.1, 0.1, distance)
g.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
g.Parent = game.Workspace
g.Anchored = true
g.CanCollide = false
g.Color = Color3.new(0.317647, 1, 0)
g.Material = Enum.Material.Neon
game.Debris:AddItem(g, 0.5)
end
print(result.Instance)
end
end)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local remote = game.ReplicatedStorage:WaitForChild("MouseEvent")
mouse.Move:Connect(function()
remote:FireServer(mouse.Hit.Position)
end)
script in serverScriptService :
local remote = game.ReplicatedStorage:WaitForChild("MouseEvent")
local visualize = true -- change to true to see the ray(s), false to not see ray(s)
local origin = nil
local direction = nil
local intersection = nil
local result = nil
local distance = nil
local range = 250
remote.OnServerEvent:Connect(function(player, mousePos)
print(player, mousePos)
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local head = character:WaitForChild("Head")
if humanoid.Health > 0 and head ~= nil then
origin = head.Position
direction = (mousePos - origin).Unit * range
result = game.Workspace:Raycast(origin, direction)
intersection = result and result.Position or origin + direction
distance = (origin - intersection).Magnitude
if visualize == true then
local ray = Instance.new("Part")
ray.Size = Vector3.new(0.1, 0.1, distance)
ray.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
ray.Parent = game.Workspace
ray.Anchored = true
ray.CanCollide = false
ray.Color = Color3.new(1, 0, 0)
ray.Material = Enum.Material.Neon
game.Debris:AddItem(ray, 0.25)
end
if result ~= nil then
print(result.Instance) -- change to what you want to detect
if result.Instance == "SCP 096" then -- in game.Workspace, this will detect the EXACT NAME OF what you want to detect
print("Detected!", typeof(result.Instance))
end
end
end
end)
Make sure there is an remote event named “MouseEvent” in replicateStorage
One small improvement is to add debounce, sanity check and more to improve the system.
Made mine that when a ray is fired to the head of enemy rig, the enemy will follow the player.Character
if result ~= nil then
if result.Instance.Name == "Head" then -- this refer to anything that are name Head!
local enemyCharacter = result.Instance.Parent
if enemyCharacter ~= nil then
local enemyHumanoid = enemyCharacter:FindFirstChild("Humanoid")
if enemyHumanoid and enemyHumanoid.Health > 0 then
enemyHumanoid:MoveTo(character.HumanoidRootPart.Position)
end
end
end
end
I am making it so that a script inside the instance’s parent (in this case the scp 096 character) gets enabled and fires a remote event but thank you very much
How would I go on to detect a part that is inside a model
(for example the detect part inside the scp 096 model)
like do something like getdescendants and if descendant name is blabla then it does this
i tried doing that but i would just break the script
(nvm i’m dumb)
I have one last question, if i’m in first person the ray gets stuck into the player’s head how would I go on to avoid making that happen as I am making a first person game. (sorry for the responses)
(Nevermind was an issue with studio I don’t know what made it happen tho)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local remote = game.ReplicatedStorage:WaitForChild("MouseEvent")
game["Run Service"].RenderStepped:Connect(function(dt)
remote:FireServer(mouse.Hit.Position)
end)