How do I use :GetPlayerFromCharacter with raycast results?

I want to make an anti-teamkill script for my raycast guns. However I have no idea on how to get the player’s team from the result. I’ve been told I need to use :Getplayerfromcharacter, then path to find the team, but I have no idea how to do it efficiently. I’ve tried to use it, but also don’t know how to make player checks

Here is my hit detection code:

	local distance = (Handle.Position - targetlocation).magnitude
	beam.Size = Vector3.new(0.1, 0.1, distance)
	beam.CFrame = CFrame.new(Handle.Position, targetlocation) * CFrame.new(0,0, -distance/2)

	game.Debris:AddItem(beam, 0.1)
	local newray = RaycastParams.new()
	local raydirection = (targetlocation - Handle.Position) * range --put a number here for the distance

	newray.FilterDescendantsInstances = {player.Character}
	-- hit detection handling

	local result = workspace:Raycast(Handle.Position, raydirection, newray)
	if result then
		if result.Instance then
			if result.Instance.Parent:FindFirstChild("Humanoid") then
				--print(result.Instance.Parent.Name)
				--print(game:GetService("Players"):FindFirstChild(result.Instance.Parent.Name).Team.Name)
				--print(result.Instance.Parent)
				local num = math.random(1,10)
				if num > 9 then
					print("miss")
					print(num)
				else
					if result.Instance.Name == "Head" then
						print(result.Instance.Parent.Head.Name)
						result.Instance.Parent.Humanoid.Health -= headshot
					else
						print(result.Instance.Name)
						result.Instance.Parent.Humanoid.Health -= damage
					end
					if result.Instance.Parent.Humanoid.Health <= 10 then
						print("low health")
						

					end
				end
				
			end
		end

Thanks in advance

local RaycastPlayer = game.Players:GetPlayerFromCharacter(result.Instance.Parent)

then check if the RaycastPlayer.Team == Player who fired.Team

if it does not match then deal damage

2 Likes