Making a Knife - Script Issues

I created a knife and now I tried to create a script to do damage to another guy but I am not succeeding. Plus, I don’t get any errors in the output.

LocalScript inside the Tool

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local damage = 50

local playerDeathEvent = ReplicatedStorage:WaitForChild("PlayerDeathEvent")

local function onPlayerAttacked(player, attacker)
	local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid:TakeDamage(damage)

		if humanoid.Health <= 0 then
			player.Character:BreakJoints()
			playerDeathEvent:FireAllClients(player)
		end
	end
end

local function onToolActivated()
	local player = Players:GetPlayerByUserId(script.Parent.Parent.Parent.UserId)
	if player then
		for _, otherPlayer in pairs(Players:GetPlayers()) do
			if otherPlayer ~= player and otherPlayer.Character then
				local distance = (otherPlayer.Character.HumanoidRootPart.Position - script.Parent.Parent.Position).Magnitude
				if distance < 10 then
					onPlayerAttacked(otherPlayer, player)
				end
			end
		end
	end
end

script.Parent.Activated:Connect(onToolActivated)

Script in ServerScriptService:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local playerDeathEvent = ReplicatedStorage:WaitForChild("PlayerDeathEvent")

playerDeathEvent.OnServerEvent:Connect(function(player)
	local attacker = player:GetAttribute("attacker")
	if attacker then
		print(attacker.Name .. " killed " .. player.Name)
	else
		print(player.Name .. " died")
	end
end)

Extra:
I ended using aswell the ChatGDT to help as my first tries didn’t suceeded but even with the AI Help, I still can’t make it happen.

2 Likes

Handle the damage on the server side.
The server wouldn’t know if the player was damaged or is dead on the client, because it wouldn’t replicate.

Don’t use Ai please, the ai just gave you an exploitable a client damaging script.

3 Likes

Even with Server , it doesn’t work

What does it say?
You should at least be able to damage the player on the server side

That’s a problem, there is no output errors, I can put some print() to help seek it. I might need tro try that.

Server script inside the tool.

local Tool: Tool? = script.Parent
local Character = script.Parent.Parent
local Player = game:GetService("Players"):GetPlayerFromCharacter(Character)
local CS = game:GetService("CollectionService")
local Handle:BasePart? = Tool:WaitForChild("Handle")
local Touched
Tool.Activated:Connect(function()
	if not CS:HasTag(Player, "Cooldown") then
		CS:AddTag(Player, "Cooldown")
		task.delay(2,function()
			CS:RemoveTag(Player, "Cooldown")
		end)
		Touched = Handle.Touched:Connect(function(Part:BasePart?)
			local PlayerReal = game:GetService("Players"):GetPlayerFromCharacter(Part)
			if PlayerReal ~= nil and PlayerReal ~= Player then
				if PlayerReal.Character and Player.Character:FindFirstChildOfClass("Humanoid").Health > 0 then
					PlayerReal.Character:FindFirstChildOfClass("Humanoid"):TakeDamage(15)
				end
			end
		end)
		task.wait(1)
		Touched:Disconnect()
	end
end)

Add prints for every line and see where it stops, should tell you where the issue is

I’ve tested and some reason doesn’t work neither. Both players are different teams.

What do you mean doesn’t work? What does not work?

Script that you sended, it doesn’t give errors neither the damage

local Tool: Tool? = script.Parent
local Character = script.Parent.Parent
local Player = game:GetService("Players"):GetPlayerFromCharacter(Character)
local CS = game:GetService("CollectionService")
local Handle:BasePart? = Tool:WaitForChild("Handle")
local Touched
Tool.Activated:Connect(function()
	if not CS:HasTag(Player, "Cooldown") then
print("activated")
		CS:AddTag(Player, "Cooldown")
		task.delay(2,function()
			CS:RemoveTag(Player, "Cooldown")
		end)
		Touched = Handle.Touched:Connect(function(Part:BasePart?)
			print("touched")
			local PlayerReal = game:GetService("Players"):GetPlayerFromCharacter(Part)
			if PlayerReal ~= nil and PlayerReal ~= Player then
				print("hit")
				if PlayerReal.Character and Player.Character:FindFirstChildOfClass("Humanoid").Health > 0 then
					PlayerReal.Character:FindFirstChildOfClass("Humanoid"):TakeDamage(15)
				end
			end
		end)
		task.wait(1)
		Touched:Disconnect()
	end
end)

Here are some debugs tell me if any prints.

Not even one print.

I’ve noticed something strange about one line.

local Touched

Should be like that?

Yes it should, it’s a connection, it wouldn’t effect anything. Can I see your tool properties?

Nevermind
|||||||||||||\|||||||||||||\|||||||||||||\|||||||||||||\

Sure.
image
image

image

Surely that you probably stop caring about this post, which its understable and I’m not answering it to bother you but just to ask just one question.

The size of the handle may be the cause of the problem?