Monster not getting destroyed and not giving coins to player who kills it?

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)
1 Like

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.

If I’m wrong, please correct me!

1 Like

I just want to reiterate what I said yesterday…

1 Like

So something like

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)

Also it didn’t print any ofthe words when I tried

You didnt put any print statements.

1 Like
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)

Try this

1 Like

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)

still didn’t print

Try what I posted in this post and tell me if it works: Monster not getting destroyed and not giving coins to player who kills it? - #7 by Quackers337

Also when I tried that it did the same thing, his health goes to 0 and he falls on the floor
coalmonster