So based off the title you probablly already know what i want to achieve. If you don’t, im trying to give player 50 cash for every kill he does. How would i do that? (btw i dont know how to use remote events)
After damaging the humanoid, put this under.
if humanoid.Health > 0 then
-- reward the player :: example use below.
yourleaderstatsthing.Cash.Value += 50
end
this will allow the player to gain money for damaging it after, so you will need to “return” the code apon the event if the heath is <= 0
if humanoid.Health <= 0 then
return
end
i already have a line that does the same thingy if OtherPart.Parent:WaitForChild("Humanoid",5).Health == 0 then
and the problem im having is that idk how to connect the thingy. (and also do it in a serverscript without using remoteevents)
So basically i don’t know how to get the leaderstats without problems.
You could use the GetPlayerFromCharacter function and from there access the leaderstats of the Player, for example:
local Players = game:GetService("Players")
local char = OtherPart.Parent
local plr = Players:GetPlayerFromCharacter(char)
local leaderstats = plr:FindFirstChild("leaderstats")
I tried
game.Players.PlayerAdded:Connect(function(plr)
local tool = script.Parent
local da = true
tool.Touched:Connect(function(OtherPart)
if OtherPart.Parent:FindFirstChild("Humanoid") and da == true then
da = false
local player = game.Players:GetPlayerFromCharacter(OtherPart.Parent)
OtherPart.Parent.Humanoid:TakeDamage(15)
script.Enabled = true
wait(1)
script.Enabled = false
wait(1)
da = true
if OtherPart.Parent:WaitForChild("Humanoid",5).Health == 0 then
plr.leaderstats.cash.Value = plr.leaderstats.cash.Value + 20
else
end
end
end)
end)
but i get: Humanoid is not a valid member of Backpack “Players.Mob1leN0OB.Backpack”
- When a player hits someone then add a boolvalue (and store it in a folder anywhere, I would suggest storing folder inside victim) and name it the player’s name ( the one that hit the victim )
local tag = Instance.new("BoolValue", playervictim.Folder) -- Note that "playervictim" is the name of the player who has gotten hit
tag.Name = Player.Name -- name this the name of the player who hit the victim
- After the victim dies, loop through all players and see if boolvalue’s name matches to a player’s name (though I’m not sure if this will work since I haven’t really tested properly)
for i, v in pairs(Players:GetPlayers()) do
if v.Name == tag.Name then
-- if it matches, reward the player
v.leaderstats.Coins.Value += 10
end
end
This has nothing to do with your “backpack” issue, I just wanted to fix your code:
local Players = game:GetService("Players")
local tool = script.Parent
tool.Touched:Connect(function(OtherPart)
local Player = Players:GetPlayerFromCharacter(OtherPart.Parent)
local Humanoid = Player.Character:FindFirstChild("Humanoid")
if Humanoid and da == true then
da = false
Humanoid:TakeDamage(15)
script.Enabled = true
task.wait(1)
script.Enabled = false
task.wait(1)
da = true
end
end)
In the future, instead of using
OtherPart.Parent.Humanoid:TakeDamage(15)
You can just do:
local Player = Players:GetPlayerFromCharacter(OtherPart.Parent)
local Humanoid = Player.Character:FindFirstChild("Humanoid")
Humanoid:TakeDamage(15)
Because "local Player = Players:GetPlayerFromCharacter(OtherPart.Parent) " you are getting the player from the parts you hit with the sword and in your script you defined this but did absolutely nothing to it.
Also why would you even put a .Touched function inside of a .PlayerAdded function anyways, this makes no sense
Also you can write a separate script and check for player’s health like this:
Humanoid.HealthChanged:Connect(function(health)
if health <= 0 then
print("Player is dead")
end
end)
Don’t do:
if OtherPart.Parent:WaitForChild("Humanoid",5).Health == 0 then
plr.leaderstats.cash.Value = plr.leaderstats.cash.Value + 20
else
end
Because this will most likely not work especially if you even put it inside of a .Touched function.