I want this script to print “hello” when the ray hits the player and print “nothing” or something when the player is behind the wall. My issue is that the script is not printing anything and when I managed to make it print sometimes by messing around with it it keeps printing “nothing”(when it prints nothing when it’s not detecting the player). I’ve looked at the roblox Developer Hub about raycasting it helped me understand it, but i’ve haven’t seen anyone do anything about getting a player when a ray hits them so i have no idea how i would go about it. i’ve also asked for help on scripting discord servers they didn’t really help me as much. pretty much what this script does without the raycasting part is just look at the player when they’re in a certain range
local part = script.Parent
local partmodel = part.Parent.Parent
local origin = part.Position
local direction = part.CFrame.LookVector * 100
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {part, partmodel}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
while wait(1) do
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Head")
if human and torso and human.Health > 0 then
if (torso.Position - part.Position).magnitude < 100 then
part.CFrame = CFrame.new(part.Position, torso.Position)
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
if raycastResult then
local hit = raycastResult.Instance
if hit.Parent:FindFirstChild("Humanoid") then
if hit and hit.Parent.Humanoid then
print("hello")
else
print("nothing")
end
end
end
end
end
end
end
1 Like
Could you try printing out what the hit
variable gives you? Your raycastResult
seems to work fine, but it isn’t detecting any Humanoid
like parents
i’ve tried to print out what it’s hitting but it’s not printing anything
if raycastResult then
print(raycastResult.Instance)
end
Could you double check that you’re really getting the Part’s Parent Parent? You might just be getting the workspace if you inserted too much Parents
:
script > Parent > Parent > Parent
I would check where the part is facing, if this is for a turret then the part is only getting the direction of the part where it starts off.
yeah i got the ray facing the right way and everything
bam. now it’s printing “Part” in output
local partmodel = workspace.Workerthrow
Well we’re getting somewhere at least Try printing the raycastResult.Instance.Parent
as well?
oh no i think i messed something up, now it’s not printing anything. i put “print(“hello?”)” and it’s printing hello and not the results
local part = script.Parent
local partmodel = workspace.Workerthrow
local origin = part.Position
local direction = part.CFrame.LookVector * 100
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {part, partmodel}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
while wait(1) do
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Head")
if human and torso and human.Health > 0 then
if (torso.Position - part.Position).magnitude < 100 then
part.CFrame = CFrame.new(part.Position, torso.Position)
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
print("hello?")
if raycastResult then
print(raycastResult.Instance.Parent)
end
end
end
end
end
You should still probably implement a else
statement if all doesn’t work, this is a odd situation to say the least:
local origin = part.Position
local direction = part.CFrame.LookVector * 100
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {part, partmodel}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
while wait(1) do
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Head")
if human and torso and human.Health > 0 then
if (torso.Position - part.Position).magnitude < 100 then
part.CFrame = CFrame.new(part.Position, torso.Position)
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
print("hello?")
if raycastResult then
local RayInstance = raycastResult.Instance
local InstanceParent = RayInstance.Parent
print(InstanceParent)
else
print("Something happened? But what?")
end
end
end
end
end
You aren’t getting any errors btw?
1 Like
I’m not getting any errors at all. so i put the else thing and it’s printing “Something happened? But what?”
Hm, so the issue could possibly lie within creating your raycastResult
variable
Is your for i, v in pairs
loop used to hit multiple players?
just one player at a time, should i end the i, v in pair loop before the raycastResult
variable?
hmm actually yeah i would like to try and make it target multiple players
Yeah try & temporary remove that, and see if anything changes?
hmm still printing “Something happened? But what?”
local part = script.Parent
local partmodel = workspace.Workerthrow
local origin = part.Position
local direction = part.CFrame.LookVector * 100
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {part, partmodel}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
while wait(1) do
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Head")
if human and torso and human.Health > 0 then
if (torso.Position - part.Position).magnitude < 100 then
part.CFrame = CFrame.new(part.Position, torso.Position)
end
end
end
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
if raycastResult then
local RayInstance = raycastResult.Instance
local InstanceParent = RayInstance.Parent
print(InstanceParent)
else
print("Something happened? But what?")
end
end
Could you try placing the origin
& direction
inside the loop?
oh my bad i read that wrong yeah it printed out my username!!! now we’re getting somewhere lol
bam now it’s printing out what type of hair i got on
Ok thank heavens I was about to throw my PC down a 50 story house
Alright, now since we have detected the Character Model we can define our Humanoid variable inside the raycastResult
check & give them damage when intercepting the ray:
if raycastResult then
local RayInstance = raycastResult.Instance
local Humanoid = RayInstance.Parent:WaitForChild("Humanoid", 3)
Humanoid:TakeDamage(10)
else
print("Something happened? But what? (That or a result has not been found)")
end
it gave me an error “Workspace.thrower.new:28: attempt to index nil with ‘TakeDamage’” the error line is at “Humanoid:TakeDamage(10)”