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.
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)
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)