(SOLVED) How would I make all explosions In my game add to a player leaderstat?

I currently making a pirate game but I have cannon’s on all the boats that emit an explosion once colliding with an object but it does not add a leaderstat to the person if they killed someone here is the code to the cannon’s:

local switch = script.Parent
local gunBarrelOne = script.Parent.Parent.GunBarrel.One

local debounce = false
local gunOne = true

local cannonBall = Instance.new("Part")
cannonBall.Size = Vector3.new(1,1,1)
cannonBall.BrickColor = BrickColor.new(1)
cannonBall.Shape = 0
cannonBall.BottomSurface = 0
cannonBall.TopSurface = 0
cannonBall.Name = "Cannon Shot"
cannonBall.Elasticity = .1
cannonBall.Reflectance = 0
cannonBall.Friction = 0

function fire(player)

	local sound = script.Parent:findFirstChild("GunSound")
	if sound == nil then
		sound = Instance.new("Sound")
		sound.Name = "GunSound"
		sound.SoundId = "http://www.roblox.com/asset?id=2101148"
		sound.Volume = 1
		sound.Parent = script.Parent
	end
	sound:play()



	
	local missile = Instance.new("Part")


	local barrel

	if gunOne == true then
		barrel = gunBarrelOne
		gunOne = true
	
	end
	
	local spawnPos = barrel.CFrame * Vector3.new(6, 0, 0)

	local dx = math.random(50,50)
	local dy = math.random(0,0)
	local dz = math.random(0,0)
	local mag = math.random(750,750)

	local v = barrel.CFrame:vectorToWorldSpace(Vector3.new(mag + dx,dy,dz))
	
	local missile = cannonBall:clone()

	missile.Position = spawnPos
	missile.Velocity = v


	
	local new_script = script.Parent.CannonBall:clone()
	new_script.Disabled = false
	new_script.Parent = missile

	local creator_tag = Instance.new("ObjectValue")
	creator_tag.Value = player
	creator_tag.Name = "creator"
	creator_tag.Parent = missile
	


	missile.Parent = game.Workspace

end

function onClicked()
	if debounce == false then
		debounce = true
		switch.BrickColor = BrickColor.new(21)
	
		fire(player)
		wait(.5)

		wait(1)
		debounce = false
		switch.BrickColor = BrickColor.new(37)
	end
end


script.Parent.ClickDetector.MouseClick:connect(onClicked)



Thanks

2 Likes

Player is never defined so it’s nil. When you call fire in onClicked, player doesn’t exist so likewise fire doesn’t receive anything as the player. ClickDetectors fire with the player that activated the detector so really it just seems like you need to add the player to the parameters of onClicked.

local function onClicked(player)

If it doesn’t work then, the next likely place to look for issues is the way you handle creator tags.

1 Like