How to add a Distance in this script

In this script when you see slender a Image appears and that, but here is a problem that there’s not a distance, like even if you see him from a long distance it will damage you

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Gui = script.Parent

local static1 = Gui:WaitForChild("static1")
local static2 = Gui:WaitForChild("static2")
local image = Gui:WaitForChild("he")

local parts = {}
parts.Slenderman = nil


local tweenService = game:GetService("TweenService")

local SlenderRootPart

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local char = player.Character
local humanoid = char:WaitForChild("Humanoid")

local angle = camera.FieldOfView

local tInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)

function FindModel()
    if workspace:FindFirstChild("Slenderman") then
        parts.Slenderman = workspace:WaitForChild("Slenderman")
        parts.Slenderman = parts.Slenderman:WaitForChild("HumanoidRootPart")
        return true
    else
        print("Can't Find"..warn("False"))
        return false
    end
end

RunService.RenderStepped:Connect(function()
    if FindModel() then
    local partsInVision = {}
    for , part in pairs (parts) do
        if math.pow((camera.CFrame.LookVector-(part.CFrame.p-camera.CFrame.p).Unit).Magnitude/2,2) < angle/360 then
            table.insert(partsInVision, part)
        end
    end
    local closestPart = nil
    local closestDistance = nil
    for , part in pairs (partsInVision) do
        if not closestPart then 
            closestPart = part 
            closestDistance = (part.CFrame.p-camera.CFrame.p).Magnitude 
            continue 
        end
        if (part.CFrame.p-camera.CFrame.p).Magnitude < closestDistance then
            closestPart = part 
            closestDistance = (part.CFrame.p-camera.CFrame.p).Magnitude
        end
    end
    if closestPart then
            --print("Slender is here"
        humanoid.Health -= 0.15
        if not static1.IsPlaying then
            static1:Play()
            static2:Play()
        end
            --if static1.PlaybackLoudness <= MaxPlaybackLoudness then
                image.ImageTransparency = math.clamp(static1.PlaybackLoudness/300, 0.1, 0.9)
            --end
    else
        if static1.IsPlaying then
                static1:Stop()
                local t = tweenService:Create(image, tInfo, {ImageTransparency = 1})
                t:Play()
            --image.ImageTransparency = 1
            if static2.IsPlaying then
                static2:Stop()
            end
        end
    end
    end
end)
1 Like

Try this, I added a variable at the top of the script called “maxDistance”. You can change the variable to whatever you want the distance to be.

ocal static1 = Gui:WaitForChild("static1")
local static2 = Gui:WaitForChild("static2")
local image = Gui:WaitForChild("he")

local maxDistance = 10

local parts = {}
parts.Slenderman = nil


local tweenService = game:GetService("TweenService")

local SlenderRootPart

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local char = player.Character
local humanoid = char:WaitForChild("Humanoid")

local angle = camera.FieldOfView

local tInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)

function FindModel()
    if workspace:FindFirstChild("Slenderman") then
        parts.Slenderman = workspace:WaitForChild("Slenderman")
        parts.Slenderman = parts.Slenderman:WaitForChild("HumanoidRootPart")
        return true
    else
        print("Can't Find"..warn("False"))
        return false
    end
end

RunService.RenderStepped:Connect(function()
    if FindModel() then
    local partsInVision = {}
    for , part in pairs (parts) do
        if math.pow((camera.CFrame.LookVector-(part.CFrame.p-camera.CFrame.p).Unit).Magnitude/2,2) < angle/360 then
            table.insert(partsInVision, part)
        end
    end
    local closestPart = nil
    local closestDistance = nil
    for , part in pairs (partsInVision) do
        if not closestPart then 
            closestPart = part 
            closestDistance = (part.CFrame.p-camera.CFrame.p).Magnitude 
            continue 
        end
        if (part.CFrame.p-camera.CFrame.p).Magnitude < closestDistance then
            closestPart = part 
            closestDistance = (part.CFrame.p-camera.CFrame.p).Magnitude
        end
    end
    if closestPart and closestDistance < maxDistance then
            --print("Slender is here"
        humanoid.Health -= 0.15
        if not static1.IsPlaying then
            static1:Play()
            static2:Play()
        end
            --if static1.PlaybackLoudness <= MaxPlaybackLoudness then
                image.ImageTransparency = math.clamp(static1.PlaybackLoudness/300, 0.1, 0.9)
            --end
    else
        if static1.IsPlaying then
                static1:Stop()
                local t = tweenService:Create(image, tInfo, {ImageTransparency = 1})
                t:Play()
            --image.ImageTransparency = 1
            if static2.IsPlaying then
                static2:Stop()
            end
        end
    end
    end
end)

Didnt work as intended, nothing in output

Ok nvm, it worked, just a tiny typo errors

1 Like

was the distance part working? or nothing at all

2 Likes