I am making it so if a player attacks a monster with a weapon, the monster gets destroyed and that players gets coins, so far it doesn’t work with no errors. Script.
local Monster = script.Parent
Monster.Touched:connect(function(p)
if p.Parent.Name == "Coal Sword" then
Monster:Destroy()
local humanoid = (p:FindFirstAncestorOfClass("Model") or p.Parent):FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
local leaderstats = player:FindFirstChild("leaderstats")
local Coins = leaderstats:FindFirstChild("Gold")
if Coins then
Coins.Value = Coins.Value + 100
end
end
end)
Have you tried adding print statements to debug where the problem is?
For example, you might do:
local Monster = script.Parent
Monster.Touched:connect(function(p)
print("Touched")
if p.Parent.Name == "Coal Sword" then
print("Touched Coal Sword")
Monster:Destroy()
local humanoid = (p:FindFirstAncestorOfClass("Model") or p.Parent):FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
local leaderstats = player:FindFirstChild("leaderstats")
local Coins = leaderstats:FindFirstChild("Gold")
if Coins then
print("Coins")
Coins.Value = Coins.Value + 100
end
end
end)
Then see where the code reaches (i.e. which is the last print statement that gets executed).
I assume it’s because you’re destroying the model, and the model contains the script over it. So if the script gets destroyed, it can’t continue to operate. So I would put Monster:Destroy() at the end, once it’s giving the coins. Although, I’m very unsure about that as I don’t usually do those kind of stuff.
local Monster = script.Parent
Monster.Touched:connect(function(p)
if p.Parent.Name == "Coal Sword" then
Monster.humanoid.Parent:Destroy()
local humanoid = (p:FindFirstAncestorOfClass("Model") or p.Parent):FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
local leaderstats = player:FindFirstChild("leaderstats")
local Coins = leaderstats:FindFirstChild("Gold")
if Coins then
Coins.Value = Coins.Value + 100
end
end
end)
local Monster = script.Parent
Monster.Touched:connect(function(p)
if p.Parent.Name == "Coal Sword" then
local humanoid = (p:FindFirstAncestorOfClass("Model") or p.Parent):FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
local leaderstats = player:FindFirstChild("leaderstats")
local Coins = leaderstats:FindFirstChild("Gold")
if Coins then
Coins.Value = Coins.Value + 100
end
Monster:Destroy()
end
end)
I put the print statements but got rid of them, then I did
local Monster = script.Parent
Monster.Touched:connect(function(p)
print("Touched")
if p.Parent.Name == "Coal Sword" then
print("Touched by sword")
Monster.humanoid.Parent:Destroy()
local humanoid = (p:FindFirstAncestorOfClass("Model") or p.Parent):FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
local leaderstats = player:FindFirstChild("leaderstats")
local Coins = leaderstats:FindFirstChild("Gold")
if Coins then
print("Coins")
Coins.Value = Coins.Value + 100
end
end
end)