I have a gun script that casts a ray when the Held value is true, if the ray detects a humanoid in it's instance's parent (ray.Instance.Parent) it damages that humanoid. But when I shoot at any object it damages a humanoid no matter what, for better comparison it's like an aimbot gun. I find this to be the weirdest thing, so I decided to print ray.Instance.Parent.Name and whenever I shoot anything it prints "BASE" in the output. I don't know why this is happening but if someone could help me that would be great! Thanks!

LocalScript inside of the gun:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Held = false
local DelayPerBullet = 0.1

script.Parent.Activated:Connect(function()
	Held = true
end)

script.Parent.Deactivated:Connect(function()
	Held = false
end)

while wait() do
	if Held == true then
		wait(DelayPerBullet)
		local mousePos = mouse.Hit.p
		script.Parent.OnShoot:FireServer(mousePos)
	end
end

ServerScript:

local Ammo = script.Ammo
Ammo.Value = 30
local MaxAmmo = script.MaxAmmo
MaxAmmo.Value = 30

game.Players.PlayerAdded:Connect(function(player)
	local Character = player.CharacterAdded:Wait()
	local Gun = Character:FindFirstChild("Gun") or player.Backpack:FindFirstChild("Gun")
	
	local Barrel = Gun.Barrel
	
	Gun.OnShoot.OnServerEvent:Connect(function(player, mousePos)
		
		if Ammo.Value <= MaxAmmo.Value then
			local midPoint = (Barrel.Position - mousePos)
				
			local ray = workspace:Raycast(midPoint, (mousePos - Barrel.Position).Unit * 100)
				
			if ray then
				local hitPart = ray.Instance
				print(ray.Instance.Parent.Name)
				if hitPart.Parent:FindFirstChild("Humanoid") then
					print("Found humanoid")
					local Humanoid = hitPart.Parent.Humanoid
					if Humanoid ~= player.Character.Humanoid then
						Humanoid:TakeDamage(10)
					end
				end
			end
		else
			print("Not enough ammo")
		end
	end)
end)

2 comments

0

which humanoid is it damaging? where did you get the script?

0

Sorted out the issue, turns out it was the origin of the ray.