How To Detect If Touching Shadows

Topic is pretty self-explanatory. If you can help, thanks!

3 Likes

Do search the Forum first before creating your Topic :slightly_smiling_face:

4 Likes

Thanks! From that I found this:

local rayDirection = game.Lighting:GetSunDirection()

local char = game.Players.LocalPlayer.Character

local RAY_LENGTH = 30

game:GetService(“RunService”):BindToRenderStep(“SunService”,Enum.RenderPriority.Camera.Value + 1,function()
local ray = Ray.new(char.HumanoidRootPart.Position,rayDirection * RAY_LENGTH)
local partFound = workspace:FindPartOnRay(ray,char)
if partFound then
print(“Found something blocking sun:”,partFound)
else
print(“No shadow”)
end
end)

game.Lighting:GetPropertyChangedSignal(“TimeOfDay”):Connect(function()
rayDirection = game.Lighting:GetSunDirection()
end)

1 Like

@JackscarIitt Are you there? I customized this script to make an AI burn in the sunlight, but it does not work. No errors in output.

local rayDirection = game.Lighting:GetSunDirection()

local char = script.Parent

local RAY_LENGTH = 30

game:GetService(“RunService”):BindToRenderStep(“SunService”,Enum.RenderPriority.Camera.Value + 1,function()

local ray = Ray.new(char.HumanoidRootPart.Position,rayDirection * RAY_LENGTH)

local partFound = workspace:FindPartOnRay(ray,char)

if not partFound then

script.Parent:BreakJoints()

end

end)

game.Lighting:GetPropertyChangedSignal(“TimeOfDay”):Connect(function()

rayDirection = game.Lighting:GetSunDirection()

end)

Could you try this debugging script? Also make sure the script is inside either the workspace or ServerScriptService

local rayDirection = game.Lighting:GetSunDirection()
local char = script.Parent
local RAY_LENGTH = 30

game:GetService("RunService"):BindToRenderStep("SunService",Enum.RenderPriority.Camera.Value + 1,function()
    print("Running")
    local ray = Ray.new(char.HumanoidRootPart.Position,rayDirection * RAY_LENGTH)
    local partFound = workspace:FindPartOnRay(ray,char)
    if not partFound then
        print("Found part")
        char:BreakJoints()
    else
        print("Not found part")
    end
end)
5 Likes

All right I will try this in a bit and tell you how it goes.

Edit: Oddly, it is now working (aside from in it’s first life before any respawns, which I can easily fix by killing it once). Thanks!

1 Like

@JackscarIitt I’m sorry but once again, I need your help. The script only works sometimes.

Can you check what the Output prints each time you head somewhere?

1 Like

Yes, I did. One the times that the script does not work, it prints nothing. On the times the script does work, however, it prints accordingly and properly.

@JackscarIitt (sorry for the ping but you weren’t responding. Do you mind pings?)

Pinging me won’t really do anything unless if I’m actually available at this point of time

So apparently I just realized that you can only use BindToRenderStep on the Client, and not the Server whoops

If you want a NPC/AI, use Heartbeat on the Server I believe:

local rayDirection = game.Lighting:GetSunDirection()
local char = script.Parent
local RAY_LENGTH = 30

print(rayDirection)
game:GetService("RunService").Heartbeat:Connect(function()
	print("Running")
	local ray = Ray.new(char.HumanoidRootPart.Position,rayDirection * RAY_LENGTH)
	local partFound = workspace:FindPartOnRay(ray,char)
	if not partFound then
		print("Found part")
		char:BreakJoints()
	else
		print("Not found part")
	end
end)
4 Likes

Thank you. Heartbeat works! :slight_smile:

1 Like