When killing player I get this error:

I need help with this script everyime I kill another player I receive this message:

--LocalScript:7: Damage is not a valid member of Folder "Players.Player2.Backpack.Gun.Scripts.(Local)"

Here’s the script:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local mouse = player:GetMouse()

local DamageEvent = script.Parent.Parent["(Local)"].Damage
local tool = script.Parent.Parent.Parent

local equipped = false

local IdleAnimId = Instance.new("Animation")
IdleAnimId.AnimationId = tool.Animations.IdleAnim.AnimationId

local ShootAnimId = Instance.new("Animation")
ShootAnimId.AnimationId = tool.Animations.ShootAnim.AnimationId

local IdleAnimation = humanoid:LoadAnimation(IdleAnimId)
local ShootAnimation = humanoid:LoadAnimation(ShootAnimId)

local SFX = tool.FirePoint.Effects.SFX

tool.Equipped:Connect(function()
   local holder = tool.Parent
   IdleAnimation:Play()
   if holder.Name == player.Name then
   	equipped = true
   	else
   	equipped = false
   end
end)

tool.Unequipped:Connect(function()
   IdleAnimation:Stop()
   local holder = tool.Parent
   if holder.Name == player.Name then
   	equipped = true
   else
   	equipped = false
   end
end)

mouse.Button1Down:Connect(function()
   if equipped then
   	script.Parent.Shoot:FireServer(mouse.Hit.p)
   	ShootAnimation:Play()
   end
end)

DamageEvent.OnClientEvent:Connect(function()
   SFX.Hitmarker:Play()
end)

and the server…

local tool = script.Parent.Parent.Parent

local firePoint = tool.FirePoint

local ShootEvent = script.Parent.Parent["(Local)"].Shoot
local DamageEvent = script.Parent.Parent["(Local)"].Damage

local Effects = firePoint.Effects
local SFX = Effects.SFX

local range = 300

local damageAmount = tool.Configs.GunComponents.Damage.Value

ShootEvent.OnServerEvent:Connect(function(player, mouseHit)
	
	SFX.Fire:Play()
	Effects.MuzzleEffect.Enabled = true
	Effects.MuzzleFlash.Enabled = true
	wait(0.05)
	Effects.MuzzleEffect.Enabled = false
	Effects.MuzzleFlash.Enabled = false

	if player.Name == tool.Parent.Name then
		local filterType = Enum.RaycastFilterType.Exclude
		local filter = RaycastParams.new()
		filter.FilterType = filterType
		filter.FilterDescendantsInstances = {tool.Parent}
		local raycast = workspace:Raycast(firePoint.Position, (mouseHit - firePoint.Position).Unit * range, filter )
		if raycast then
			local hitPart = raycast.Instance
			print(hitPart.Name)
			local char = hitPart.Parent
			local hum = char:FindFirstChild("Humanoid")
			if char.Name ~= tool.Parent.Name and hum then
				hum:TakeDamage(damageAmount)
				DamageEvent:FireClient(player)
			end
		end
	end
end)
1 Like

Since you’re trying to get the damage remote as soon as the player joins the game, it may not have loaded in yet. Change it to local DamageEvent = script.Parent.Parent["(Local)"]:WaitForChild("Damage").

I tested it out and that worked but now it says this:

--LocalScript:18: Cannot load the AnimationClipProvider Service.

Referring to how the animation plays

I’m not getting this error when I’m testing, but this post suggests adding game:GetService('RunService').Stepped:Wait() before loading the animation. Let me know if that fixes it.
Remember that you can paste the error in the search bar and chances are someone has already asked about the same error.

Sorry for responding late but that solution did not work, as I forgot to mention that…

Everytime, it gives this error is when the player dies and the gun and the script resets. So maybe you could help me find a solution to that problem.

1 Like

Apologies for my very late response. Apparently Humanoid:LoadAnimation has been depreciated in favor of Animator:LoadAnimation. Not sure if it will fix your error if you change it, but try this:

local animator = humanoid:WaitForChild("Animator")
local IdleAnimation = animator:LoadAnimation(IdleAnimId)
local ShootAnimation = animator:LoadAnimation(ShootAnimId)

See the announcement here for more information.

1 Like