Hello, i’ve tried many time to make a kill for cash script, it works with the roblox sword, but not with my melee, and my fruit skills, can someone help me ?
Do you have anything in place currently that you can share?
i have the leaderstat script
local datastore = game:GetService("DataStoreService")
local data1 = datastore:GetDataStore("coins")
game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Coins = Instance.new("IntValue", leaderstats)
Coins.Name = "Coins"
Coins.Value = data1:GetAsync(player.UserId) or 0
data1:SetAsync(player.UserId, Coins.Value)
game.Players.PlayerRemoving:connect(function()
data1:SetAsync(player.UserId, Coins.Value)
end)
end)
This is likely because Roblox’s sword puts a “Tag” on the humanoid the sword hit called “creator”. Tools don’t have this by default. In order to make it so you can kill a humanoid for cash/money you need to “tag” the humanoid using an ObjectValue
called “creator” and check when the humanoid dies if they were tagged and who tagged them.
-- These functions were directly taken from Roblox's ClassicSword Script
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
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
so, i put this script in my tool, but its still not giving any coins, btw here is the script that give coins
local datastore = game:GetService("DataStoreService")
local data1 = datastore:GetDataStore("Coins")
game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Coins = Instance.new("IntValue", leaderstats)
Coins.Name = "Coins"
Coins.Value = data1:GetAsync(player.UserId) or 0
data1:SetAsync(player.UserId, Coins.Value)
game.Players.PlayerRemoving:connect(function()
data1:SetAsync(player.UserId, Coins.Value)
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function(killed)
local CreatorTag = character.Humanoid:FindFirstChild("creator")
if CreatorTag and CreatorTag.Value then
local stats = CreatorTag.Value:WaitForChild("leaderstats")
stats["Coins"].Value = stats["Coins"].Value + 20
end
end)
end)
end)
end)
Does your tool script tag the humanoid?
i put your script in the tool so i mean i guess
You need to use the functions for the tag to work. Functions are called by using something like this
function MyFunction()
-- Do stuff
end
MyFunction()
In your case you would need to use the TagHumanoid()
function when the player gets hit by the tool.
Make a folder called “Rewards” inside of the npc,
then create a number value inside of the “Rewards” folder.
The number value should be named “Coins”, then you can give that
number value a value as the reward.
Then grab the classic sword and go inside of the SwordScript,
then goto line 70 and implement this code inside of it:
function TagHumanoid(humanoid, player)
if not humanoid.Parent:FindFirstChild("LootTags") then
local lootTags = Instance.new("Folder", humanoid.Parent)
lootTags.Name = "LootTags"
end
local tag = Instance.new("Model", humanoid)
tag.Name = player.Name
game:GetService("Debris"):AddItem(tag, 1)
local lootTag = Instance.new("Model", humanoid.Parent.LootTags)
lootTag.Name = player.Name
game:GetService("Debris"):AddItem(lootTag, 20)
end
--[[ 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 --]]
Line 118 can be made as a comment…
Then make a new script inside of the npc’s model, and
implement this code inside that new script this time:
local players = game:GetService("Players")
local humanoid = script.Parent.Humanoid
humanoid.HealthChanged:Connect(function()
local function Loot()
local alreadyGotLoot = {}
for _, tag in pairs(humanoid.Parent:FindFirstChild("LootTags"):GetChildren()) do
if not table.find(alreadyGotLoot, tag.Name) then
table.insert(alreadyGotLoot, tag.Name)
local player = players:FindFirstChild(tag.Name)
if player then
player.leaderstats.Coins.Value += humanoid.Parent:FindFirstChild("Rewards").Coins.Value
end
end
end
end
if humanoid.Health == 0 then
wait(0.15)
Loot()
game:GetService("Debris"):AddItem(humanoid.Parent, 1.5)
end
end)
Hopefully it all works for you, already tested it myself…
I mean when i kill a player not a npc
You never specified before, I would suggest trying to maybe use what I’ve already provided and then try make the system by yourself because I’m busy with my own game…
i dont understand