Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Raycast gun increasing the players kills for every shot instead of once when the shot player dies?

In my game, I have a gun that uses raycasting. I want to give the player +1 "VampireKills" when the vampire they shoot dies. But instead of doing the intended function, the player gains +1 kills for every shot they landed beforehand. Another problem is that when the player shoots a vampire and they die by any other means, the player will still get the kill(s). Help would be appreciated. Heres a snippet of the gun's code. The function that gives them a kill is located at line 59.

if result then
		local part = result.Instance
			local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
			local hitbox = part
		local torso = part.Parent:FindFirstChild("Torso") or part.Parent.Parent:FindFirstChild("Torso")
		local root = part.Parent:FindFirstChild("HumanoidRootPart") or part.Parent.Parent:FindFirstChild("HumanoidRootPart")
		local charactervalues = part.Parent:FindFirstChild("charactervalues") or part.Parent.Parent:FindFirstChild("charactervalues")
			local material = part.Material
			
			if part.Name == "Part" or part.Name == "Union" or part.Name == "Wall" or part.Name == "Mattress" or part.Name == "Eqa" or part.Name == "Handle" or part.Name == "Lever" or part.Name == "Union" or part.Name == "Bulb" or part.Name == "Light" or part.Name == "Lever1" or part.Name == "Lever2" or part.Name == "Metal1" or part.Name == "Sand" or part.Name == "Arm" or part.Name == "Floor" or part.Name == "Frame" or part.Name == "Glass" or part.Name == "Paint" or part.Name == "Seat" or part.Name == "VehicleSeat" or part.Name == "Wedge" or part.Name == "Baseplate" or part.Name == "Base" or part.Name == "Crane" or part.Name == "Forklift" or part.Name == "MeshPart" or part.Name == "Hook" or part.Name == "Pallet" or part.Name == "Monitor 3" or part.Name == "RopeCapRoundBase" or part.Name == "RopeShaftRoundSmoothBase" or part.Name == "Trashcan" or part.Name == "Support" or part.Name == "Vending Machine" or part.Name == "Ambient Light" or part.Name == "Attach" or part.Name == "Main" or part.Name == "Pillar" or part.Name == "Truss" or part.Name == "IndoorWall" or part.Name == "Bathroom doors" or part.Name == "door frame" then
				if part.Material == Enum.Material.Plastic or part.Material == Enum.Material.SmoothPlastic or part.Material == Enum.Material.Neon or part.Material == Enum.Material.Fabric then
				
					local plastic = game.ServerStorage.bullethits.PlasticHit:Clone()
					plastic.Parent = game.Workspace.Thrown
					
				plastic.Position = result.Position
					plastic.Sound.PlaybackSpeed = 	random:NextNumber(0.5, 1.5)
					plastic.Script.Enabled = true
					plastic.Sound:Play()
				elseif part.Material == Enum.Material.Asphalt or part.Material == Enum.Material.Basalt or part.Material == Enum.Material.Brick or part.Material == Enum.Material.Cobblestone or part.Material == Enum.Material.Concrete or part.Material == Enum.Material.CrackedLava or part.Material == Enum.Material.Ground or part.Material == Enum.Material.Granite or part.Material == Enum.Material.Slate or part.Material == Enum.Material.Sandstone or part.Material == Enum.Material.Ice or part.Material == Enum.Material.Rock or part.Material == Enum.Material.Salt or part.Material == Enum.Material.Pebble or part.Material == Enum.Material.Marble or part.Material == Enum.Material.Limestone or part.Material == Enum.Material.Pavement or part.Material == Enum.Material.Glacier then

					local rock = game.ServerStorage.bullethits.RockHit:Clone()
					rock.Parent = game.Workspace.Thrown
					rock.Position = result.Position
					rock.Sound.PlaybackSpeed = 	random:NextNumber(0.5, 1.5)
					rock.Script.Enabled = true
					rock.Sound:Play()
				elseif part.Material == Enum.Material.CorrodedMetal or part.Material == Enum.Material.DiamondPlate or part.Material == Enum.Material.Foil or part.Material == Enum.Material.Metal then

					local metal = game.ServerStorage.bullethits.MetalHit:Clone()
					metal.Parent = game.Workspace.Thrown
					metal.Position = result.Position
					metal.Sound.PlaybackSpeed = 	random:NextNumber(0.5, 1.5)
					metal.Script.Enabled = true
					metal.Sound:Play()
				elseif part.Material == Enum.Material.Wood or part.Material == Enum.Material.WoodPlanks then

					local wood = game.ServerStorage.bullethits.WoodHit:Clone()
					wood.Parent = game.Workspace.Thrown
					wood.Position = result.Position
					wood.Sound.PlaybackSpeed = 	random:NextNumber(0.5, 1.5)
					wood.Script.Enabled = true
					wood.Sound:Play()
					
				elseif part.Material == Enum.Material.Sand then

					local sand = game.ServerStorage.bullethits.SandHit:Clone()
					sand.Parent = game.Workspace.Thrown
					sand.Position = result.Position
					sand.Sound.PlaybackSpeed = 	random:NextNumber(0.5, 1.5)
					sand.Script.Enabled = true
					sand.Sound:Play()

				end
				end
			
			if hitbox and hitbox.Name == "Hitterbox" then
				local hitted = hitbox.Parent.Parent
				--give the player a kill
				hitted.Humanoid.Died:Connect(function()
					if humanoid.Parent:FindFirstChild("charactervalues") then
						player.challengedata.VampireKills.Value += 1
					end
				end)
				--[[
				if charactervalues:WaitForChild("Role").Value ~= "Vampire" and humanoid ~= user2 then
					user2:TakeDamage(40)
					game.ServerStorage.Dummy:Clone().Parent = playa.PlayerGui
				end
				]]
				if charactervalues:WaitForChild("Role").Value ~= "Vampire" or humanoid.PlatformStand == true or humanoid == user2 then 
					user2:TakeDamage(0)
				else
					humanoid:TakeDamage(60)
					torso.BloodSplatter.Enabled = true
					spawn(function()
						wait(0.1)
						torso.BloodSplatter.Enabled = false
					end)
					torso.BloodSplatter.Enabled = true
					spawn(function()
						wait(0.1)
						torso.BloodSplatter.Enabled = false
					end)
				local sound = game.ServerStorage.Hit:Clone()
				sound.Parent = root
				sound.PlaybackSpeed = 	random:NextNumber(0.5, 1.5)
				sound:Play()
				spawn(function()
					wait(3)
					sound:Destroy()
					end)
				
				end
			end

			
		end

	
	

	wait(0.25)
		bullet_clone:Destroy()
		end
end)

1 answer

✓ Accepted answer

you forgot to put an end after local hitted = hitbox.Parent.Parent. This should work. on line 1, put hitted = nil so its a global variable. and that way, instead of local hitted you can just do hitted. I don't believe that it is possible to call functions inside of if statements. If you have to remove an end from the code, it would be best to so that way it's the function itself instead of being stored in an if statement.

 if hitbox and hitbox.Name == "Hitterbox" then
                local hitted = hitbox.Parent.Parent
                    end
                --give the player a kill
                hitted.Humanoid.Died:Connect(function()
                    if humanoid.Parent:FindFirstChild("charactervalues") then
                        player.challengedata.VampireKills.Value += 1
                    end
                end)

This discussion is preserved as it was. New answers, comments, and votes are closed.