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

Knife throwing works on localscript but not serverscript?

Hey all,

I'm attempting to deconstruct the code of a freemodeled knife so that I don't have to worry about coming up with my own math for throwing a knife in the direction a player is facing myself. The code is the exact same on both the localscript (free model original) and the serverscript (the version I'm converting). For whatever reason in the server script the knife keeps travelling behind the player , but when using the localscript it shoots in the correct direction. I have no knowledge of CFrame or anything to do with positions so your help in narrowing the problem is greatly appreciated.

In the following GIF the first knife in my inventory is the server sided knife, and the second is the free modeled local knife:

https://gyazo.com/4b032820b2499661d9abbb382e0cd7df

The following code is the same in both knifes, all variables have been changed to be properly defined considering one is local and one is server sided, the variables are not the problem I'm sure of that.

function ThrowAttack()
	KnifeOut()
	if PlayThrow then
		PlayThrow.Value = true
		wait(0.3)
		if not Handle then
			return
		end
		local throwingHandle = Handle:Clone()
		DebrisService:AddItem(throwingHandle, 5)
		throwingHandle.Parent = workspace
		if MyCharacter and MyHumanoid then
			throwingHandle.Velocity = (MyHumanoid.TargetPoint - throwingHandle.CFrame.p).unit * 100
			-- set the orientation to the direction it is being thrown in
			throwingHandle.CFrame = CFrame.new(throwingHandle.CFrame.p, throwingHandle.CFrame.p + throwingHandle.Velocity) * CFrame.Angles(0, 0, math.rad(-90))
			local floatingForce = Instance.new('BodyForce', throwingHandle)
			floatingForce.force = Vector3.new(0, 196.2 * throwingHandle:GetMass() * 0.98, 0)
			local spin = Instance.new('BodyAngularVelocity', throwingHandle)
			spin.angularvelocity = throwingHandle.CFrame:vectorToWorldSpace(Vector3.new(0, -400, 0))
		end
		Handle.Transparency = 1
		-- Wait so that the knife has left the thrower's general area
		wait(0.08)
		if throwingHandle then
			local touchedConn = throwingHandle.Touched:connect(function(hit)
				BlowDamage(hit, DAMAGE)
				throwingHandle:Destroy()
			end)
		end
		-- must check if it still exists since we waited
		if throwingHandle then
			throwingHandle.CanCollide = true
		end
		wait(0.6)
		if Handle and PlayThrow then
			Handle.Transparency = 0
			PlayThrow.Value = false
		end
	end
	KnifeUp()
end

Thank you for your help.

1 comment

0

It won't let us view the gif

1 answer

✓ Accepted answer

Humanoid.TargetPoint doesn't replicate to the server. You are best off firing an event to the server with where the player has clicked so you could throw it towards there.

1 comment

0

thank you!

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