Hello devs! I am currently creating my first raycast gun and it had ran into an error that does not display in the output…
I am unsure whether the ray is even being created. What it is doing is not damaging the player…
Here is my code:
local replicatedStorage = game:GetService("ReplicatedStorage")
local fireBullet = replicatedStorage:WaitForChild("FireBullet")
local tool = script.Parent
fireBullet.OnServerEvent:Connect(function(player, damagePerHit)
local rayOrigin = tool.Muzzle.Position
local rayDestination = Vector3.new(0, 0, 100)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDestination, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
hitPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local humanoid = hit.Parent.Humanoid
humanoid:TakeDamage(damagePerHit)
end
end)
end
end)
For this you don’t use the touched methode. Instead, you say:
if hitpart.Parent:FindFirstChild("Humanoid") then
local humanoid = hitpart.Parent.Humanoid
humanoid:TakeDamage(damagePerHit)
end
Implemented this in you’re code, it would look lile this:
local replicatedStorage = game:GetService("ReplicatedStorage")
local fireBullet = replicatedStorage:WaitForChild("FireBullet")
local tool = script.Parent
fireBullet.OnServerEvent:Connect(function(player, damagePerHit)
local rayOrigin = tool.Muzzle.Position
local rayDestination = Vector3.new(0, 0, 100)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDestination, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
if hitpart.Parent:FindFirstChild("Humanoid") then
local humanoid = hitpart.Parent.Humanoid
humanoid:TakeDamage(damagePerHit)
end
end
end)
This is because the raycast doesn’t need a touched methode, it already checks for the object by itselfs.
tool.Activated:Connect(function()
if not isReloading and not fireDebounce then
currentAmmo = currentAmmo - 1
print(currentAmmo)
text.Text = currentAmmo .. "/" .. ammoCapacity
print(mouse.Hit.p)
if currentAmmo ~= 0 then
fireBullet:FireServer(mouse.Hit.p, damagePerHit) <--
else
print("No more ammo")
isReloading = true
end
end
end)