So this is a bit of a complicated code but here goes. So first i made this part that follows the camera to cast light, and made it transfer to the server for shared lighting. Then i decided to add a monster that would be scared off by light, however when i try using a raycast from directly infront of the light part, it doesnt register unless the camera touches a part. Anyone have any idea why?
--Flashlight Local Script
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local light = game.ReplicatedStorage.Light
light.Parent = workspace.Debris
light.CFrame = workspace.CurrentCamera.CFrame
light.Ring.Transparency = -math.huge
game.UserInputService.InputBegan:Connect(function(key, processed)
if processed then return end
if key.KeyCode == Enum.KeyCode.F then
game.ReplicatedStorage.RemoteEvents.UpdateLightValue:FireServer()
light.ActiveSound:Play()
if light.SpotLight.Enabled == true then
light.SpotLight.Enabled = false
light.SpotLight2.Enabled = false
else
light.SpotLight.Enabled = true
light.SpotLight2.Enabled = true
end
end
end)
char:WaitForChild("serverlight")
char.serverlight:Destroy()
while true do
light.CFrame = workspace.CurrentCamera.CFrame
game.ReplicatedStorage.RemoteEvents.UpdateLightPosition:FireServer(light.CFrame)
task.wait()
end
-- Flashlight Server Script
local slight = game.ReplicatedStorage.Light
local events = game.ReplicatedStorage.RemoteEvents
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local clone = slight:Clone()
clone.Name = "serverlight"
clone.Parent = character
end)
end)
events.UpdateLightValue.OnServerEvent:Connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
local light = char.serverlight
light.ActiveSound:Play()
if light.SpotLight.Enabled == true then
light.SpotLight.Enabled = false
light.SpotLight2.Enabled = false
else
light.SpotLight.Enabled = true
light.SpotLight2.Enabled = true
end
end)
events.UpdateLightPosition.OnServerEvent:Connect(function(player, cframe)
local char = player.Character or player.CharacterAdded:Wait()
char.serverlight.CFrame = cframe
end)
--Monster Script (Serversided)
local reach = {}
local TweenService = game:GetService("TweenService")
local currentEntities = workspace.CurrentEntities
local serverStorage = game.ServerStorage
local assets = serverStorage.Assets
local entities = assets.Entities
local reachModel = entities.Reach
local spawnRoom = 1
local spawnTable = nil
local touchedEvent = nil
local alreadyAttacked = false
function reach.New(currentRoom, generatedRooms, entityName)
alreadyAttacked = false
spawnRoom = currentRoom+1
while not generatedRooms[spawnRoom]:FindFirstChild("Furniture"):FindFirstChild("ShortTable") do
spawnRoom +=1
end
spawnTable = generatedRooms[spawnRoom].Furniture:WaitForChild("ShortTable")
if spawnTable.ReachSpawn.CFrame:ToAxisAngle().Z ~= 0 then return end
local currentReach = reachModel:Clone()
local root = currentReach.PrimaryPart
root.CFrame = spawnTable.ReachSpawn.CFrame
currentReach.Parent = currentEntities
local rs = game:GetService("RunService")
local light = script.Parent
currentReach.Hitbox.Touched:Connect(function(hit)
if alreadyAttacked then return end
if hit.Parent:FindFirstChild("Humanoid") and (hit.Name == "Left Leg" or hit.Name == "Right Leg") then
alreadyAttacked = true
reach.Attack(currentReach, hit.Parent, hit)
end
end)
for i, object in workspace:GetDescendants() do
if object.Name == "serverlight" then
while currentReach.Parent do
task.wait(0.01)
local params = RaycastParams.new()
local filtertable = {object}
params.FilterType = Enum.RaycastFilterType.Exclude
for i, plr in game.Players:GetChildren() do
if plr.Character then
table.insert(filtertable, plr.Character)
end
end
params.FilterDescendantsInstances = {filtertable}
local cast = workspace:Raycast(object.Position, object.CFrame.LookVector, params)
if cast then
print(cast.Instance)
local hitmarker = Instance.new("Part")
hitmarker.Position = cast.Position
hitmarker.Anchored = true
hitmarker.CanCollide = false
hitmarker.Size = Vector3.new(1,1,1)
hitmarker.Parent = workspace.Debris
end
end
end
end
end
function reach.Attack(currentReach, reachTarget, reachAnimationTarget)
reachTarget.Humanoid.WalkSpeed = 0
task.wait(0.25)
reach.Stretch(currentReach, reachTarget, reachAnimationTarget)
task.wait(4.75)
reachTarget.Humanoid.WalkSpeed = 15
end
function reach.Stretch(currentReach, reachTarget, reachAnimationTarget)
local currentReachArm = currentReach.ReachBody["Right Arm"]
currentReachArm.Anchored = true
local startPos = currentReachArm.Position
local direction = (reachAnimationTarget.Position - startPos).Unit
local distance = (reachAnimationTarget.Position - startPos).Magnitude
local thickness = 1
local sizeZ = 1
local goalSize = Vector3.new(thickness, thickness, distance)
local baseCFrame = CFrame.new(startPos, reachAnimationTarget.Position)
currentReachArm.Size = Vector3.new(thickness, thickness, 1)
currentReachArm.CFrame = baseCFrame * CFrame.new(0, 0, -sizeZ / 2)
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local proxy = Instance.new("NumberValue")
proxy.Value = 1
proxy:GetPropertyChangedSignal("Value"):Connect(function()
if currentReachArm and currentReachArm.Parent then
local currentZ = proxy.Value
currentReachArm.Size = Vector3.new(thickness, thickness, currentZ)
currentReachArm.CFrame = baseCFrame * CFrame.new(0, 0, -currentZ / 2)
end
end)
local tween = TweenService:Create(proxy, tweenInfo, { Value = distance })
tween:Play()
task.delay(4, function()
local retractTween = TweenService:Create(proxy, tweenInfo, { Value = 1 })
retractTween:Play()
end)
end
return reach