Raycasting Guns

So I am making a fighting games but where you fight NPC’s and I have been doing experiments and I a NPC kill for cash leaderboard and when you kill an NPC you get rewarded with 15 cash. My problem/experiment is that when I kill a NPC with the default roblox sword it gives me cash. But when I do it with the guns I made and certain guns from the toolbox it does nothing. How could I make it to where when I kill an NPC with ANY weapon it counts for cash. Here is the leaderstats script:

 game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local Cash = Instance.new("IntValue", leaderstats)
	Cash.Name = "Cash"
end)


Yes a very basic leaderboard.
Here is the NPC's Cash script:


script.Parent.Zombie.Died:connect(function() --The NPC's Humanoid is named Zombie
	local tag = humanoid:FindFirstChild("creator")
	if tag ~= nil then
		if tag.Value ~= nil then
			local leaderstats = tag.Value:FindFirstChild("leaderstats")
			if leaderstats ~= nil then
				leaderstats.Cash.Value = leaderstats.Cash.Value + 15
				wait(0)
			end
		end
	end
Thanks
1 Like

what is “tag.Value”, is that the name of the player or is an Object Value that is the player/

Do your guns have a script to tag the zombie?

Yes but they do not work for some reason

But in your guns, once you detect that a player has killed a zombie, does an ObjectValue even get inserted into the zombie?

What is the value of the tag, the player’s name or the player itself?

They each have different kinds and each gun works differently

The player is the tag.Value, Value

Any errors? Also could you add print statements after each “if statement” so that we know where the code breaks.

No there has been no errors it’s just for some reason different guns wont register the scripts and just do nothing. Which is the problem

I have also learned that melee weapons like swords and stuff have a better outcome percentage. But guns have a low percentage chance of working. I would like to have more guns than melee weapons.

For example, let’s say you wanted to tag the zombie after the player has shot it.

function OnHit(hit)
    if hit.Parent then
        local hum = hit.Parent:FindFirstChild("Humanoid")
        if hum then
           --Do Damage
           
           -- Add a tag to the zombie
           local tag = Instance.new("ObjectValue",hit.Parent)
           tag.Name = "creator"
           tag.Value = plr -- Player who fired the gun
        end
    end
end

If you want to add a tag only when the player has KILLED the zombie, you can do this instead:

function OnHit(hit)
    if hit.Parent then
        local hum = hit.Parent:FindFirstChild("Humanoid")
        if hum then
           --Do Damage
       
           -- Add a tag to the zombie
           if hum.Health <= 0 then --Player has killed the zombie because Health is lower than 0
               local tag = Instance.new("ObjectValue",hit.Parent)
               tag.Name = "creator"
               tag.Value = plr -- Player who fired the gun
           end
        end
    end
end

I just tried the first one and it did nothing

Did you define what humanoid is?

local tag = humanoid:FindFirstChild("creator")

Also, I’m assuming you have multiple zombies, so when you use this:

script.Parent.Zombie.Died:connect(function()

script.Parent.Zombie could point to the first instance of child named “Zombie” in the workspace(assuming your script is in the workspace)

I just defined what humanoid is and still nothing with both scripts

Also the zombie with the scripts is in RelicatedStorage because I have spawners and a wave system that handles them

Why are you checking if the zombie died in the leaderstats script? You should make a script that is in the zombie, checking if the zombie died.

Instead of using
script.Parent.Zombie.Humanoid.Died

use
script.Parent.Humanoid.Died

Wait, also you are calling .Died on the Zombie itself? .Died is an event fired by the humanoid, so you have to do script.Parent.Zombie.Humanoid.Died instead.