For some reason, the raycast isn’t working on my gun. It either returns nil or (very rarely) the players right arm. I don’t know how, but I wanted to make a part go across the raycast to visually make sure it’s going in the right direction, but I don’t know how. If anyone has any ideas then please help.
local script:
local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()
--animations
local equipAnim = humanoid:LoadAnimation(script:WaitForChild("equip"))
local holdAnim = humanoid:LoadAnimation(script:WaitForChild("hold"))
local reloadAnim = humanoid:LoadAnimation(script:WaitForChild("reload"))
local shootAnim = humanoid:LoadAnimation(script:WaitForChild("shoot"))
--settings
local guntype = "Automatic" --Automatic, Semi-Auto, 3-Round Burst
local firerate = 0.1 --in seconds
local damage = 20
--variables
local equipped = false
local reloading = false
local shooting = false
local mousedown = false
--remote event
local remote = tool:WaitForChild("serverEvent")
--equipped and unequipped
tool.Equipped:Connect(function()
equipped = true
equipAnim:Play()
tool.Unequipped:Connect(function()
equipped = false
equipAnim:Stop()
end)
end)
tool.Equipped:Connect(function()
wait(0.3)
holdAnim:Play()
tool.Unequipped:Connect(function()
holdAnim:Stop()
end)
end)
--AUTOMATIC WEAPONS
mouse.Button1Down:Connect(function()
mousedown = true
while mousedown do
if equipped and not reloading then
shootAnim:Play()
shooting = true
remote:FireServer(mouse.Hit.Position, damage)
end
wait(firerate)
shootAnim:Stop()
end
end)
mouse.Button1Up:Connect(function()
mousedown = false
end)
server script
local tool = script.Parent
local event = tool:WaitForChild("serverEvent")
local shootPart = tool:WaitForChild("gunmodel"):WaitForChild("BulletExit")
event.OnServerEvent:Connect(function(player, mosPos, dmg)
local char = player.Character or player.CharacterAdded:Wait()
local localHuman = char:WaitForChild("Humanoid")
local result = workspace:Raycast(shootPart.Position, mosPos)
print(result)
if result then
local part = result.Instance
local h = part.Parent:FindFirstChild("Humanoid")
if h and not localHuman then
h:TakeDamage(dmg)
end
end
end)
ah, i see the problem. you are not providing a set of RaycastParams.
try replacing the server script with this. it should work properly.
local tool = script.Parent
local event = tool:WaitForChild("serverEvent")
local shootPart = tool:WaitForChild("gunmodel"):WaitForChild("BulletExit")
event.OnServerEvent:Connect(function(player, mosPos, dmg)
local char = player.Character or player.CharacterAdded:Wait()
local localHuman = char:WaitForChild("Humanoid")
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {char:GetChildren()}
local result = workspace:Raycast(shootPart.Position, mosPos, params)
print(result)
if result then
local part = result.Instance
local h = part.Parent:FindFirstChild("Humanoid")
if h and not localHuman then
h:TakeDamage(dmg)
end
end
end)
in the modified script, i’ve added a blacklist, and the only thing in that blacklist is the children of the character. that way, it will not consistently return any of the children of your character.
i used that code. It’s the same result. I’m thinking it may have something to do with animation in my gun? if that makes any sense. Here’s a video.
(also i’m really sorry about the audio, i forgot to turn off my spotify so you’ll have to listen to Alex G while watching)
i believe that it should function now (it does no damage if accessories are hit, also, the ray now comes out of an attachment found in the tool’s handle)
client script:
local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()
--animations
local equipAnim = humanoid:LoadAnimation(script:WaitForChild("equip"))
local holdAnim = humanoid:LoadAnimation(script:WaitForChild("hold"))
local reloadAnim = humanoid:LoadAnimation(script:WaitForChild("reload"))
local shootAnim = humanoid:LoadAnimation(script:WaitForChild("shoot"))
--settings
local guntype = "Automatic" --Automatic, Semi-Auto, 3-Round Burst
local firerate = 0.1 --in seconds
local damage = 20
--variables
local equipped = false
local reloading = false
local shooting = false
local mousedown = false
--remote event
local remote = tool:WaitForChild("serverEvent")
--equipped and unequipped
tool.Equipped:Connect(function()
equipped = true
equipAnim:Play()
tool.Unequipped:Connect(function()
equipped = false
equipAnim:Stop()
end)
end)
tool.Equipped:Connect(function()
task.wait(0.3)
holdAnim:Play()
tool.Unequipped:Connect(function()
holdAnim:Stop()
end)
end)
--AUTOMATIC WEAPONS
mouse.Button1Down:Connect(function()
mousedown = true
while mousedown do
if equipped and not reloading then
shootAnim:Play()
shooting = true
remote:FireServer(mouse.Hit.Position, damage)
end
task.wait(firerate)
shootAnim:Stop()
end
end)
mouse.Button1Up:Connect(function()
mousedown = false
end)
server script:
local tool = script.Parent
local event = tool:WaitForChild("serverEvent")
local handle = tool:WaitForChild("Handle")
local shootPart = handle:WaitForChild("BulletExit")
event.OnServerEvent:Connect(function(player, mosPos, dmg)
local char = player.Character or player.CharacterAdded:Wait()
local localHuman = char:WaitForChild("Humanoid")
-- calculate ray direction
local rayDirection = (mosPos - shootPart.WorldPosition).Unit
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {tool, char}
local rayStart = shootPart.WorldPosition
local rayEnd = rayStart + rayDirection * 100 -- extend the ray for 100 studs
local result = workspace:Raycast(rayStart, rayDirection * 100, raycastParams)
-- print("raycast hit:", result)
if result then
local hitPart = result.Instance
local hitHumanoid = hitPart.Parent:FindFirstChild("Humanoid")
if hitHumanoid and hitHumanoid ~= localHuman then
hitHumanoid:TakeDamage(dmg)
end
else
print("ray did not hit anything.")
end
end)