You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Im trying to make a raycast gun
What is the issue? For some reason it doesn’t detect the humanoid
What solutions have you tried so far? I tried looking on devforum, but I didn’t find anything
This is my first time trying to make a raycast gun, this is the script that handles the Raycast
local rs = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local WeaponShoot = rs.Events:WaitForChild("WeaponShoot")
WeaponShoot.OnServerEvent:Connect(function(plr, origin, direction, damage)
print("Event Recived")
local char = plr.Character
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {char}
local result = workspace:Raycast(origin, direction, Params)
if result and result.Instance then
print("Got Result And Instance")
local hitCharacter = result.Instance:FindFirstAncestorWhichIsA("Model")
if hitCharacter then
print("Got Character")
local hitHum = hitCharacter:FindFirstChild("Humanoid")
if hitHum then
print("Got Hum")
hitHum:TakeDamage(damage)
end
end
end
end)
and this is the local script inside the tool that activates the WeaponShoot
local rs = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local WeaponShoot = rs.Events:WaitForChild("WeaponShoot")
local Mouse = Players.LocalPlayer:GetMouse()
local Tool = script.Parent
local StartPos = Tool:WaitForChild("ShootStart")
local Config = Tool.Configuration
local Damage = Config.Damage
local Delay = Config.Delay
local MaxAmmo = Config.MaxAmmo
local ReloadTime = Config.ReloadTime
Tool.Activated:Connect(function()
WeaponShoot:FireServer(StartPos.Position, Mouse.Hit.Position, Damage.Value)
end)
Adding into this, Mouse.Hit.Position is not a directional Vector3 that faces where you clicked at. To get the directional vector that faces where you clicked at, you create a CFrame using CFrame.lookAt(at, lookAt). Basically, what this function does is create a CFrame that is positioned at at and it is facing towards the position lookAt.
Now, to get the directional vector, we simply index it with .LookVector
local directionVector = CFrame.lookAt(StartPos.Position, Mouse.Hit.Position).LookVector
Now we can replace the second argument with directionVector
local rs = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local WeaponShoot = rs.Events:WaitForChild("WeaponShoot")
local Mouse = Players.LocalPlayer:GetMouse()
local Tool = script.Parent
local StartPos = Tool:WaitForChild("ShootStart")
local Config = Tool.Configuration
local Damage = Config.Damage
local Delay = Config.Delay
local MaxAmmo = Config.MaxAmmo
local ReloadTime = Config.ReloadTime
Tool.Activated:Connect(function()
local directionVector = CFrame.lookAt(StartPos.Position, Mouse.Hit.Position).LookVector
WeaponShoot:FireServer(StartPos.Position, directionVector, Damage.Value)
end)
Hmm, have you tried making the FindFirstChild() recursive? Just put in true as the second argument. Some suggestions are: please use guard clause!
Example of guard clause:
-- bad code
if conditionA then
if conditionB then
-- do something
end
end
-- guard clause code (good)
if not conditionA then
return
end
if not conditionB then
return
end
-- do something 2
Guard clause prevents too much indentations, which makes it more readable (also so your eyes don’t have to jump one line per if statement!)