Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I use a table in a raycast to see if a certain limb has been hit?

I've been trying to make a gun using Raycasting and so far I have this...

Server:

local HeadshotSound = script.Parent.Pistol.Pistol.Headshot

local LimbsTable = {"Head", "Left Leg", "Right Leg", "Left Arm", "Right Arm"}

script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)

	local raycastParams =  RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(script.Parent.Pistol.Bolt.Position,(mousePos - script.Parent.Pistol.Bolt.Position)*300,raycastParams)
	
	if raycastResult then
		local hitPart = raycastResult.Instance
		local model = hitPart:FindFirstAncestorOfClass("Model")
		
		if model and hitPart.Name == "Head" then
			model.Humanoid.Health -= 50
			HeadshotSound:Play()
		end
		
		if model and hitPart.Name ~= "Head" then
			model.Humanoid.Health -= 25
		end
		
		if model and hitPart.Name == LimbsTable then
			print("A Limb has been hit")
		end
		
	end
end)

Client:

local mouse = game.Players.LocalPlayer:GetMouse()
local UIS = game:GetService("UserInputService")
local StarterGUI = game.StarterGui
local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player.PlayerGui

local FireSound = script.Parent.Pistol.Pistol.Fired
local ReloadSound = script.Parent.Pistol.Pistol.Reload
local NoAmmoSound = script.Parent.Pistol.Pistol.NoAmmo

local MouseIcon = "http://www.roblox.com/asset/?id=6398507484"

local MaxAmmo = 6
local CurrentAmmo = 6

script.Parent.Activated:Connect(function()
	if CurrentAmmo > 0 then
		script.Parent.Fire:FireServer(mouse.Hit.p)
		FireSound:Play()
		CurrentAmmo = CurrentAmmo - 1
		PlayerGui.PistolAmmo.Frame.TextLabel.Text = CurrentAmmo .. "/6"
	else
		NoAmmoSound:Play()
	end
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		ReloadSound:Play()
		CurrentAmmo = MaxAmmo
		wait(1.376)
		PlayerGui.PistolAmmo.Frame.TextLabel.Text = CurrentAmmo .. "/6"
	end
end)

script.Parent.Equipped:Connect(function()
	mouse.Icon = MouseIcon
	PlayerGui.PistolAmmo.Frame.Visible = true
end)

script.Parent.Unequipped:Connect(function()
	mouse.Icon = ""
	PlayerGui.PistolAmmo.Frame.Visible = false
end)

I want to make it so I can see if a limb or what limb has been hit with the raycast. I created a table with the names of the limbs and when I shoot the limbs I get no output. Unless a specify the limb directly.

1 answer

✓ Accepted answer

Line 26 of your server-side code is the issue.

if model and hitPart.Name == LimbsTable then

hitPart.Name and LimbsTable do not share the same data type. hitPart.Name is a string object, and LimbsTable is a table object. That being said, you have an array in which you're checking for which limb is struck by the ray. You may use square brackets [] to specify the value in LimbsTable that will be compared to the name of the part:

if model and LimbsTable[hitPart.Name] then

If the value specified is not found in LimbsTable, the condition will not pass because the value you're trying to look for doesn't exist (it is nil in the table).

3 comments

0

I've changed the code to this https://i.imgur.com/zxdYs9C.png. When I shoot a limb it still does not print "A limb has been hit" instead it prints "A limb has not been hit".

0

You may have to use a generic loop, then.

0

Okay thankyou that worked

This discussion is preserved as it was. New answers, comments, and votes are closed.