First of all, before reading this code excuse me for the sloppiness. I haven't made it the prettiest yet, I've just been trying to figure out what is causing every error I've come across. I thought I would be able to get away with making this post short, and posting clips of my code. But, I'm afraid I have to post the entire script because I have no idea what is causing this problem ... at all. At first, I thought it was just with enemy npcs. But than, out of nowhere it started to happen with players too. Here are some clips of what the glitch looks like.

https://gyazo.com/c653bb787e75f166a68d75d6c12c9bbb

https://gyazo.com/7a91bf436fa805fc5712dd4a4fd7d992

Here is the code I'm using.


usable = true
game.ReplicatedStorage.Events.RayCastEvent.OnServerEvent:Connect(function(Player, FP, TP)

	local PlayerTeam = Player.Team
	local var = workspace:FindFirstChild(Player.Name)

	print(var)
	if Player.PlayerGui.AmmoGui.Rounds.Value <= 30 and game.Workspace:FindFirstChild(Player.Name).Humanoid.Health > 0 and usable == true then 
		usable = false 

		if Player.PlayerGui.AmmoGui.Rounds.Value ~= 0 then 
			Player.PlayerGui.AmmoGui.Rounds.Value = Player.PlayerGui.AmmoGui.Rounds.Value - 1
			Player.PlayerGui.AmmoGui.AmmoF.Ammo.Text = (Player.PlayerGui.AmmoGui.Rounds.Value..'/'..Player.PlayerGui.AmmoGui.MaxAmmo.Value)
			print(Player)
			local RayCast = Ray.new(FP, (TP-FP).unit*999999)
			local Part, Position, Normal = game.Workspace:FindPartOnRay(RayCast, Player.Character, false, true)

			local gunshot = game.ReplicatedStorage.Audios.Gunshot:Clone()

			local Bullet = game.ReplicatedStorage.Bullet:Clone()
			
			local Dist = (TP-FP).magnitude
			Bullet.CFrame = CFrame.new(FP, Position)*CFrame.new(0,0, Dist/10)
			local Dist = (TP-FP).magnitude
			gunshot.Parent = Bullet
			gunshot:Play()
			Bullet.Parent = workspace
			
			local force = Instance.new("BodyThrust")
			force.Force = Vector3.new(0, 0, -180)
			force.Parent = Bullet
			Bullet.Anchored = false
			Bullet.Transparency = 0

			local PlayerVariableTest = game.Workspace:FindFirstChild(Player.Name)
			


	

		local function onPartTouched(otherPart)
				local Hum = Part.Parent:FindFirstChild("Humanoid")
				
				if Hum ~= PlayerVariableTest.Humanoid then
					Hum:TakeDamage(math.random(7,15))
					Bullet:Destroy()
				end
			end

				local Hum = Part.Parent:FindFirstChild("Humanoid")
				if Hum and game.Players:FindFirstChild(Hum.Parent.Name) and PlayerTeam ~= game.Players:FindFirstChild(Hum.Parent.Name).Team then do
						Bullet.Touched:Connect(onPartTouched)	
					end
				elseif Hum and game.Players:FindFirstChild(Hum.Parent.Name) == nil and game.Workspace:FindFirstChild(Hum.Parent.Name) then do
						local Hum = Part.Parent:FindFirstChild("Humanoid")

						if Hum ~= PlayerVariableTest.Humanoid then
							Hum:TakeDamage(math.random(7,15))
							Bullet:Destroy()
						end
					end

				end







			end	

			
			wait(1.5)
			force:Destroy()



			wait(2)
			Bullet:Destroy()

		end


	end	
	usable = true
end)

Now, one thing to note is that when I add a wait before the "Bullet:Destroy()", of course it waits to remove the bullet. But, I can't have that because it looks odd when a player is damaged before the bullet even reaches him. As you can see, I'm using BodyThrust for the bullet. Another thing to note is that when I change the BodyThrust value to my distance between where my gun is, and where my mouse is clicking, the bullet will wait until it reaches said player or npc but when it is shooting an npc if I shoot the head its like the bullet stretches out and tries to follow the npc? I can't get a clip, it won't happen anymore. I'm all around confused about this. But I would assume it has something to do with me using BodyThrust.

If anyone knows the problem, suggestions would be appreciated.

3 comments

0

Where, in the explorer, is this script, and is it a local or server script?

0

It is a server script, activated when it is called on by a local script in a tool. Player clicks with gun in hand > code fires remote > bullet should come out but instead it just damages the player/npc.

0

I'm unsure if this is related to your problem, but it sounds like it. Try looking into Network Ownership https://developer.roblox.com/en-us/articles/Network-Ownership