I Need Help Making A Script For My Tool If I Kill Someone My Kill leaderstats Go Up
Have you searched on youtube? Humanoid Events, leaderstats? I know someone will spoon feed you the script in which good for them and you but I suggest digging into research and toying around. I will link a few articles. We were all beginners and I wish you good luck and to keep learning!
https://developer.roblox.com/en-us/api-reference/class/Humanoid
https://developer.roblox.com/en-us/api-reference/class/Player
ive already looked om youtube for 3 hours i still cant find anything its useless
If you don’t want to spoon feed then at least tell him how to implement it
i honestly feel like giving up scripting its just not fun because i cant learn:(
You don’t learn scripting just from googling what you want to make, you need to understand language syntax and programming concepts. You can learn everything you need to know about lua on the Developer Hub and on lua.org
Have you started anything yet? If you have, could you please show the code for your script (for the tool)?
i havent started making a script for the tool yet
Okay, so what kind of tool are you trying to make? A gun, a sword… etc?
im making a punching tool for boxing game
I see. So the way you want the tool to work is if a player clicks/taps on their screen while the tool is equipped. And when they do that, a punching animation will play along with a sound effect. If the player who punches touches another player (by their fists), they get damaged and will get eliminated after multiple punches on the targeted player. The player who eliminated the target will get an increase in Kills on the leaderboard.
Is this how you want your punching tool to function?
ive already done that but when i kill them i want the kill leaderstat to go up
Oh, I thought you said you didn’t make a script for the tool yet. If you already have, could you show the code for that script?
this is the damage script:
local debounce = false
script.Parent.Activated:Connect(function()
script.Parent.Handle.Touched:Connect(function(hit)
if debounce then return end
debounce = true
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - game.Players[script.Parent.Parent.Name].leaderstats.Strength.Value
end
wait(1)
debounce = false
end)
end)
Alright i see now. We need to make 2 separate functions in order for kills to count on your leaderboard.
@Fallguysgod1234567 So the first thing we need to do is to make a function of creating an ObjectValue (which will go into the humanoid when someone dies from the punch).
Here is the code for the function that creates the object value:
local Debris = game:GetService("Debris")
local function TagHumanoid(humanoid, player) -- humanoid is the target, and player is the dealer (who eliminated the target)
local Creator_Tag = Instance.new("ObjectValue")
Creator_Tag.Name = "creator"
Creator_Tag.Value = player
Debris:AddItem(CreatorTag, 2) -- how long the tag will stay before it is automatically destroyed
Creator_Tag.Parent = humanoid
end
Now we do the second function, which will basically remove the Object Value if there currently is any.
Code to the second function:
local function UntagHumanoid(humanoid)
-- checks if "creator" tag still exists in the humanoid
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy() -- destroys it if it does exist
end
end
end
So here is what your full script should look like now:
Code:
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local debounce = false
local function TagHumanoid(humanoid, player)
local Creator_Tag = Instance.new("ObjectValue")
Creator_Tag.Name = "creator"
Creator_Tag.Value = player
Debris:AddItem(Creator_Tag, 2)
Creator_Tag.Parent = humanoid
end
local function UntagHumanoid(humanoid)
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
-- the main part of the script
Tool.Activated:Connect(function()
local dealerPlayer = Players:GetPlayerFromCharacter(tool.Parent)
Handle.Touched:Connect(function(targetHit)
if debounce then
return
end
debounce = true
if targetHit.Parent:FindFirstChild("Humanoid") then
local targetHum = targetHit.Parent:FindFirstChild("Humanoid")
if dealerPlayer then
local amount = dealerPlayer.leaderstats:FindFirstChild("Strength").Value
UntagHumanoid(targetHum)
TagHumanoid(targetHum, dealerPlayer)
targetHum:TakeDamage(amount)
end
end
task.wait(1)
debounce = false
end)
end)
Now, to detect kills, you should make another Server Script but place it inside ServerScriptService. Then you may use the following code:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
local tag = humanoid:FindFirstChild("creator")
if tag then
local dealer = tag.Value
dealer.leaderstats:FindFirstChild("Kills").Value += 1
end
end)
end)
end)
And that should be it! This took some time, so hopefully this helps. Let me know if there are any issues.
You’re literally connecting the same event multiple times everytime you deal damage to someone, which can cause client/server lag.
This can be fixed by making a table of humanoids and check if the humanoid is in the table, if not then connect the function and set the event in the table. And when the player leaves, then disconnect it by using the table.
Here’s the script with my implementation that i’ve just explained it to you:
local humanoids = {}
Tool.Activated:Connect(function()
local dealerPlayer = Players:GetPlayerFromCharacter(tool.Parent)
Handle.Touched:Connect(function(targetHit)
if debounce then
return
end
debounce = true
if targetHit.Parent:FindFirstChild("Humanoid") then
local targetHum = targetHit.Parent:FindFirstChild("Humanoid")
if dealerPlayer then
local amount = dealerPlayer.leaderstats:FindFirstChild("Strength").Value
UntagHumanoid(targetHum)
TagHumanoid(targetHum, dealerPlayer)
targetHum:TakeDamage(amount)
end
if humanoids[targetHit.Parent.Name] == nil then
humanoids[targetHit.Parent.Name] = targetHum.Died:Connect(function()
local tag = targetHum:FindFirstChild("creator")
if tag then -- if creator tag exists
local dealer = tag.Value -- gets the killer
-- counts the kill
dealer.leaderstats:FindFirstChild("Kills").Value += 1
end
end)
end
end
task.wait(1)
debounce = false
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
if humanoids[plr.Name] ~= nil then
humanoids[plr.Name]:Disconnect()
humanoids[plr.Name] = nil
end
end)
Assuming this is a local script, you cannot globally increment values in a local script.
@Fallguysgod1234567 Have you tried using the scripts I have given? I havent heard any response from you for a while.