Help With getting player (:GetPlayerByCharacter)

Hello, I’m trying to make my weapon add to the kills value when the player kills a player. I tried my best, and I’m struggling to reward the player their kills. How can I make it work?

Main Part of the code:

function hitDetection(hit,ray, mousepos)
	local model = hit:FindFirstAncestorOfClass("Model")
	if model then
		local character = hit.Parent
		local humanoid = model:FindFirstChildWhichIsA("Humanoid")
		if humanoid then
			if hit.Name == "Head" then
				humanoid:TakeDamage(configuration.HeadShotDamage)
				local player = game.Players:GetPlayerFromCharacter(character)
				if humanoid.Health == 0 then
					player.leaderstats:FindFirstChild("Kills").Value += 1
					player:FindFirstChild("RoundKills").Value += 1
				end
			else
				humanoid:TakeDamage(configuration.Damage)
				local player = game.Players:GetPlayerFromCharacter(character)
				if humanoid.Health == 0 then
					player.leaderstats:FindFirstChild("Kills").Value += 1
					player:FindFirstChild("RoundKills").Value += 1
				end
			end
		else
			return true
		end
	end
end

Full Code:

local configuration = require(script.Parent.Settings)
local debris = game:GetService('Debris')
local tool = script.Parent

equipped = false
script.Parent.Equipped:Connect(function()
	equipped = true
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
end)

----------dont change-----------
local fired = false
local firedTime = 0
local eventTime = 0
local fireRate = 0
--------------------------------

function countCheck()
	if firedTime-eventTime >= fireRate or fireRate-(firedTime-eventTime) <= 1/60 then
		return true
	end
end

script.Parent.Shot.OnServerEvent:Connect(function(player,mousePos)
	if not player.Character == script.Parent.Parent then return end
	
	firedTime = tick()
	if equipped and countCheck() then
		eventTime = tick()
		script.Parent.Handle.Fire:Play()
		script.Parent.Handle.Attachment.PointLight.Enabled = true
		script.Parent.Handle.Attachment.MuzzleFlash.Enabled = true
		script.Parent.Handle.Attachment.MuzzleSmoke.Enabled = true
		for bulletCount = 1, configuration.Bullets do
			task.spawn(fire,player,mousePos)
		end
		wait(0.05)
		script.Parent.Handle.Attachment.MuzzleFlash.Enabled = false
		script.Parent.Handle.Attachment.MuzzleSmoke.Enabled = false
	elseif fired == false then
		fired = true
		task.wait(fireRate-(firedTime-eventTime))
		fired = false
	end
end)

function newBullet()
	local bullet = game.ReplicatedStorage.GunSystem.Bullet:Clone()
	bullet.Position = tool.Fire.Position
	bullet.CanCollide = false
	bullet.CastShadow = false
	bullet.Size = Vector3.new(1,1,1)
	bullet.Name = "bullet"
	bullet.Transparency = 1
	bullet.Parent = workspace.Bullets
	bullet:SetNetworkOwner(nil)
	return bullet
end

function changeGravity(bullet,mass)
	local lift = Instance.new("VectorForce",bullet)
	lift.Force = Vector3.new(0,mass*game.Workspace.Gravity,0) --gravity
	local attachment = Instance.new("Attachment",bullet)
	lift.RelativeTo = Enum.ActuatorRelativeTo.World
	lift.ApplyAtCenterOfMass = true
	lift.Attachment0 = attachment
end

function applyPhysics(bullet,mousePos)
	local muzzleVelocity = configuration.BulletVelocity
	local direction = CFrame.new(bullet.Position,mousePos)
	
	local verticalSpread = configuration.Spread
	local horizontalSpread = configuration.Spread
	local rng = Random.new()
	verticalSpread = rng:NextNumber(-verticalSpread,verticalSpread)
	horizontalSpread = rng:NextNumber(-horizontalSpread,horizontalSpread)
								
	verticalSpread = math.rad(verticalSpread)
	horizontalSpread = math.rad(horizontalSpread)
	direction = direction*CFrame.Angles(verticalSpread,horizontalSpread,0)
	direction = direction.LookVector
	
	local mass = bullet:GetMass()
	bullet:ApplyImpulse(direction * mass * muzzleVelocity)
	
	changeGravity(bullet,mass)
end

function rayFilter(player)
	local rayFilter = RaycastParams.new()
	rayFilter.FilterDescendantsInstances = {player.Character,workspace.Bullets,workspace.NoCollide}
	rayFilter.FilterType = Enum.RaycastFilterType.Blacklist
	return rayFilter
end

function updateTracer(tracer,lastPos,rayPos)
	tracer.Size = Vector3.new(.1, .1, (lastPos-rayPos).magnitude)
	tracer.CFrame = CFrame.new(rayPos,lastPos)*CFrame.new(0, 0, -(lastPos-rayPos).magnitude/2)
end

function shellejection()
	if configuration.ShellType == 1 then
		local shell = game.ReplicatedStorage.GunSystem.Shell1:Clone()
		shell.CFrame =  script.Parent.Ejection.CFrame
		shell.Velocity = script.Parent.Ejection.CFrame.lookVector * 26 + Vector3.new(0, 5, 0)
		shell.RotVelocity = Vector3.new(-10,40,30)
		shell.CanCollide = false
		shell.Parent = game.Workspace.Shells
	elseif configuration.ShellType == 2 then
		local shell = game.ReplicatedStorage.GunSystem.Shell2:Clone()
		shell.CFrame =  script.Parent.Ejection.CFrame
		shell.Velocity = script.Parent.Ejection.CFrame.lookVector * 26 + Vector3.new(0, 5, 0)
		shell.RotVelocity = Vector3.new(-10,40,30)
		shell.CanCollide = false
		shell.Parent = game.Workspace.Shells
	elseif configuration.ShellType == 3 then
		local shell = game.ReplicatedStorage.GunSystem.Shell3:Clone()
		shell.CFrame =  script.Parent.Ejection.CFrame
		shell.Velocity = script.Parent.Ejection.CFrame.lookVector * 26 + Vector3.new(0, 5, 0)
		shell.RotVelocity = Vector3.new(-10,40,30)
		shell.CanCollide = false
		shell.Parent = game.Workspace.Shells
	end
end

function hitDetection(hit,ray, mousepos)
	local model = hit:FindFirstAncestorOfClass("Model")
	if model then
		local character = hit.Parent
		local humanoid = model:FindFirstChildWhichIsA("Humanoid")
		if humanoid then
			if hit.Name == "Head" then
				humanoid:TakeDamage(configuration.HeadShotDamage)
				local player = game.Players:GetPlayerFromCharacter(character)
				if humanoid.Health == 0 then
					player.leaderstats:FindFirstChild("Kills").Value += 1
					player:FindFirstChild("RoundKills").Value += 1
				end
			else
				humanoid:TakeDamage(configuration.Damage)
				local player = game.Players:GetPlayerFromCharacter(character)
				if humanoid.Health == 0 then
					player.leaderstats:FindFirstChild("Kills").Value += 1
					player:FindFirstChild("RoundKills").Value += 1
				end
			end
		else
			return true
		end
	end
end

function fire(player,mousePos)
	local bullet = newBullet()
	applyPhysics(bullet,mousePos)
	shellejection()
	
	local rayFilter = rayFilter(player)
	
	local lastPos = bullet.Position
	local range = configuration.MaxRange
	while range > 0 do
		task.wait()

		local rayLength = (lastPos-bullet.Position).Magnitude
		range = range - rayLength
		local rayPos = bullet.Position
		local rayDirection = CFrame.new(lastPos,rayPos).lookVector
		
		--updateTracer(tracer,lastPos,rayPos)

		local ray = workspace:Raycast(lastPos,rayDirection*rayLength,rayFilter) 
		if ray then
			bullet:Destroy()
			local hit = ray.Instance
			rayPos = ray.Position

			--updateTracer(tracer,lastPos,rayPos)
			
			if hitDetection(hit,ray, mousePos) then
				script.Parent.Shot:FireClient(player)
			end
			break
		end
		if bullet.Parent == nil then
			bullet:Destroy()
			break
		end
		lastPos = bullet.Position
	end
	if bullet then
		bullet:Destroy()
	end
end

Help soon,

papahetfan

I don’t see where you define “character”, maybe try replacing that with tool.Parent.

Or replace the top 11 lines with this:

local configuration = require(script.Parent.Settings)
local debris = game:GetService('Debris')
local tool = script.Parent
local character

equipped = false
script.Parent.Equipped:Connect(function()
	equipped = true
        character = tool.Parent
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
end)

If I’m wrong, simply reply.

1 Like

I just tested it. It does seem to be working.