Cannot kill player!

Server:

local tiger = script.Parent
local humanoid = tiger:WaitForChild("Humanoid")

game.ReplicatedStorage.Attack.OnServerEvent:Connect(function(player)
	print(0)
	if player then
		print(1)
		local target = player.Character
		local distance = (tiger.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
		if distance <= 8 and target and not tiger then
			print(2)
			tiger.Head.AttackSound:Play()
			local playerDeath = game.ReplicatedStorage:WaitForChild("playerdeath")
			local player = game.Players:GetPlayerFromCharacter(target)
			playerDeath:FireClient(player,tiger)
			local attackAnim = humanoid:LoadAnimation(script.Attack)
			attackAnim:Play()
			attackAnim.Stopped:Wait()
			target.Humanoid.Health = 0
		end
	end
end)


Local Script:

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		print("Attacked")
		game.ReplicatedStorage.Attack:FireServer()
	end
end)

When I press Q it prints everything except 2!

2 Likes

why are you checking if tiger doesn’t exist?

Tiger should exist because you’ve defined it at the very top of your code:

Most likely, that’s why your code is failing. You’re checking if tiger does not exist.

1 Like

Yeah because when I don’t check it I can kill myself!

1 Like

tiger is the parent of the script, plus you’re referencing tiger in the if statement too, it doesn’t make sense for tiger to not exist.

Your code says, if the distance is less than or equal to 8 AND target exists AND tiger does not exist THEN do stuff. But tiger will ALWAYS exist as long as this script is running because tiger is the parent of the script. If there is no parent to your script then your script does not exist in the game.

Remove your check for tiger and it will run as expected.

that doesn’t work it won’t print 2 :frowning:

that will allow me to kill myself!

when you fire a remote event to the server, the player is always going to be the client who fired the event. therefore, you’re simply checking if MyPlayer is not equal to MyPlayer. I would recommend looping through all the players.

local tiger = script.Parent
local humanoid = tiger:WaitForChild("Humanoid")

game.ReplicatedStorage.Attack.OnServerEvent:Connect(function(player)
	print(0)
	for i,player in pairs(game.Players:GetChildren()) do
		print(1)
		local target = player.Character
		local distance = (tiger.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
		if distance <= 8  and target then
			print(2)
			tiger.Head.AttackSound:Play()
			local playerDeath = game.ReplicatedStorage:WaitForChild("playerdeath")
			local player = game.Players:GetPlayerFromCharacter(target)
			playerDeath:FireClient(player,tiger)
			local attackAnim = humanoid:LoadAnimation(script.Attack)
			attackAnim:Play()
			attackAnim.Stopped:Wait()
			target.Humanoid.Health = 0
		end
	end
end)

like that?

you should probably remove the player parameter, as it may interfere with the other player value. yeah, that should look good.

local tiger = script.Parent
local humanoid = tiger:WaitForChild("Humanoid")

game.ReplicatedStorage.Attack.OnServerEvent:Connect(function(player)
	print(0)
	for i,v in pairs(game.Players:GetChildren()) do
		print(1)
		local target = player.Character
		local distance = (tiger.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
		if distance <= 8  and target then
			print(2)
			tiger.Head.AttackSound:Play()
			local playerDeath = game.ReplicatedStorage:WaitForChild("playerdeath")
			local player = game.Players:GetPlayerFromCharacter(target)
			playerDeath:FireClient(player,tiger)
			local attackAnim = humanoid:LoadAnimation(script.Attack)
			attackAnim:Play()
			attackAnim.Stopped:Wait()
			target.Humanoid.Health = 0
		end
	end
end)

so that?

woah woah, please dont do that now, you will figure it out

hold on, can the player attack the tiger? or is it the tiger attacking the player? but dont do that, replace v with player, and replace the player referenced in the onserverevent with nothing. (you should also use GetPlayers() instad)

I want that the tiger can attack the player!

yeah now I can kill myself again :frowning:

check if target ~= tiger, and it should check if target is not equal to the tiger.

1 Like

Bro you are a legend it worked :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.