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

Why When I throw my Knife , Collisions are different for Server and Client?

I created A Knife Throwing Script and It works

-The Projectile is a 'Model' called Knife

  • Inside that Model is basiclly the knife mesh and a Part That Is the Same Size as the entire knife Model , and that Part is called the 'HitBox' !!!

when the Knife is thrown , It will Take A Clone of the Knife Model And Position it to the Player Facing the Mouse

Then I Add Body Velocity To the Knife To make it fly twords its Look Vector (Or the Mouses Position) !

I Also Added a Touched Event For the Hit Box to See if It Hits an Object that has its Anchored == true And its CanCollide == true (Stopping All Psychics to the knife!

The Knife Is thrown Correctly But when It touches an Anchored and CanCollided Part It Stops Perfectly "On The Server" but it leaves "On the Client" It looks like it stopped on air , and It is close to the part it hit but it is not even touching it!

Here is the Client Code! /I left Some Code out on Purpose Because its very long! [Its still readable !]

` tool.Activated:Connect(function()
--print(Stats.Used)
--print(Stats.Throwing)
if Stats.Used == false and hum.Health > 0 then
---	print("Attack passed 1!")
	Stats.Used = true
	if not Stats.Throwing then
		Stats.Used = true
		---print("Attack passed 2!")
		KifeAttackAnimation:Play()
		
	Remotes.AttackEvent:FireServer()--Swings the Knife!
	
	wait(Stats.Cooldown)
	Stats.Used = false	
				
	elseif Stats.Throwing then
		Stats.Used = true
			----print("Throw passed 1!")
		if char:WaitForChild("Knives").Value > 0 then
			---	print("Throw passed 2!")
			
		ThrowAnimation:Play()
		
	Remotes.ThrowEvent:FireServer(mouse.hit)---Throws the Knife !
	
		end	
			wait(Stats.Cooldown)
	Stats.Used = false	
	end
	
end

end) `

And then the Server Code!

` Remotes.ThrowEvent.OnServerEvent:Connect(function(plr,mousehit)

spawn(function()
	wait(.5)
	if ThrowingHitConnection then
		ThrowingHitConnection:Disconnect()
		ThrowingHitConnection = nil
	end
end)

local Hithum = false

local char = plr.Character or plr.CharacterAdded:Wait()

local hum = char:WaitForChild("Humanoid")

if hum.Health > 0 and char:WaitForChild("Knives").Value > 0 then
	
	THrowFX:Play()
	
	char:WaitForChild("Knives").Value = char:WaitForChild("Knives").Value  - 1
	
	local Projectile = LibraryModels.DefaultKnifeModel:Clone()
	
	Projectile.Parent = game.Workspace
	
	local Prime = Projectile.PrimaryPart
	
	Prime.CFrame = tool.Handle.CFrame 
	
	Prime.CFrame = CFrame.new(Prime.Position,mousehit.p) * CFrame.Angles(math.rad(90),0,math.rad(90))---Makes it look at th mouse and rotates it so the 'knife point' is pointed to the mouse
	
	
	local bv = Instance.new("BodyVelocity")--Creates the boday Velocity
	bv.Parent = Prime
	bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	bv.Velocity = -Prime.CFrame.RightVector * 3
	
	
	
	Prime.CFrame  = Prime.CFrame  * CFrame.Angles(math.rad(180),0,math.rad(90))--Rotates it


	
	Projectile.HitBox.Touched:Connect(function(hit)
		local ehum =  hit and hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
			if ehum and ehum ~= hum then---If it hit something that has a humanoid in it then , it hitted a Character Model!
				if not Hithum then
					Hithum = true
				
				ehum:TakeDamage(100)--- Make them Take Damage!
				
				local StabClone = StabFX:Clone()
				StabClone.Parent = ehum.Parent.HumanoidRootPart
				
				StabClone:Play()
				
			bv:Destroy()
			
			Projectile:Destroy()
			
			end
			
			elseif hit.Anchored == true and hit.CanCollide == true then---We know it is a sturdy objects!
				if not Hithum then
				Hithum = true
				
				warn(hit.Name)
				print(hit.Parent.Name)
				wait()
				bv:Destroy()
				
				Prime.Anchored = true----- Make it Anchored! Stops All Physics for the Part
				Prime.Position = Prime.Position
				end
			
		end
	end)
	
	game.Debris:AddItem(Projectile,30)
	
		
	for i,v in pairs(Projectile:GetChildren()) do
		if v:IsA("BasePart") then
			v:SetNetworkOwner(nil)
		end
	end
	
	
	end

end) `

1 comment

0

Your Server Code can use a delay(.5, function) instead.

1 answer

#:SetNetworkOwner

This sets the network owner in both client and server which makes it smoother for each client ingame.

Sadly you cant do this with models so an alternative is to set it's primarypart to a content of the model and change the network owner of the primary part.

Tool.PrimaryPart = Tool.Handle

Tool.Handle:SetNetworkOwner(nil)

And that's about it!

#Stay safe and have a great day

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