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

Projectiles occasionally move in the wrong direction, what did I do wrong?

I've been having trouble with my game in that sometimes when a projectile fires it will go in a random direction instead of where my mouse is pointing.

Edit: The camera is forced in its position in the example of what's happening, I'm not sure if that changes anything.

This is the local script

mouseDown = false

local Mouse = game.Players.LocalPlayer:GetMouse()
	
Mouse.Button1Down:Connect(function()
	mouseDown = true
	local Char = game.Players.LocalPlayer.Character
	local trigger = Char:FindFirstChild("MouseDown")
	print(trigger)
	print(Char)
	repeat
		print("Mouse down")
		trigger:FireServer(Mouse.Hit.p)
		print("Trigger fired")
		wait()
	until mouseDown == false
end)
	
Mouse.Button1Up:Connect(function()
	mouseDown = false
end)

This is the server script

local event = script.Parent.MouseDown
local cd = .1
local waiting = false
print("event")
event.OnServerEvent:Connect(function(plr,pos)
	if not waiting then
		waiting = true
		local part = Instance.new("Part")
		part.Position = pos
		
		local proj = Instance.new("Part")
		proj.Size = Vector3.new(1,1,4)
		proj.BrickColor = BrickColor.new("Royal purple")
		proj.Material = "Neon"
		proj.CanCollide = false
		proj.Anchored = false
		proj.Position = script.Parent.HumanoidRootPart.Position
		
		local bf = Instance.new("BodyForce")
		bf.Force = Vector3.new(0, workspace.Gravity * proj:GetMass(), 0)
		bf.Parent = proj
		
		proj.CFrame = CFrame.new(script.Parent.HumanoidRootPart.Position,Vector3.new(part.Position.X,script.Parent.HumanoidRootPart.Position.Y,part.Position.Z))
		
		bf = Instance.new("BodyVelocity")
		bf.Velocity = proj.CFrame.lookVector * 50
		bf.Parent = proj
		
		game.Debris:AddItem(proj,2)
		
		proj.Parent = workspace
		wait(cd)
		waiting = false
	end
end)

This is what's happening.

2 answers

Hmmm. Looking at it, I am almost certain it's a problem with networking.

Basically, in Roblox when an unanchored object gets close to a player, Roblox gives that player the responsibility of the object (to help drop some load of the server), that leads to lag, inconsistency in position and velocity (and perhaps some hacking) which may very well appear as you can see in the gif you sent.

The solution for that is SetNetworkOwner if you call this function from a BasePart which is not anchored (otherwise it will error) with a nil parameter, the server will never let a player take control of it thus no more hopping and stuff like that.

Here's a cool article about it: Network Ownership

5 comments

0

It still appears to go in the wrong direction occasionally. I doubt this changes anything but I'm play testing in studio

0

hmmm, I have tested your code myself, and while it's not as severe as yours. I have tried adding a .Unit to the bf.Velocity = proj.CFrame.lookvector and it seems like it was fixed. Let me know if it's fixed.

0

So, proj.CFrame.LookVector.Unit * 50? If that's what you meant then it doesn't seem to have fixed the issue unfortunately.

0

Exactly what I meant. After a lot of debugging, I came with my final conclusion and it's that, it's the input. I have tested it with what I thought was a completely frozen mouse but turned out not to be and it had very tiny differences in values which got bigger when used as a projectile.

0

What I recommend you to do, is to instead (just for testing) bind the function to a keyboard key and leave the mouse completely unmoved, the input should be the same and thus same projectile direction etc. If not then unfortunately, that's how far I can try to help. I hope you find the solution :D

I've managed to fix the issue through the help of this thread

I've only changed the local script significantly from the original

mouseDown = false

local UserInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera

local Mouse = game.Players.LocalPlayer:GetMouse()
	
Mouse.Button1Down:Connect(function()
	mouseDown = true
	local Char = game.Players.LocalPlayer.Character
	local trigger = Char:FindFirstChild("MouseDown")
	print(trigger)
	print(Char)
	repeat
		print("Mouse down")
		print(Mouse.Hit.p)
		local v = UserInputService:GetMouseLocation()
		trigger:FireServer(test(v.X,v.Y))
		print("Trigger fired")
		wait()
	until mouseDown == false
end)
	
Mouse.Button1Up:Connect(function()
	mouseDown = false
end)

function test(x,y)
	local unitray = camera:ViewportPointToRay(x, y)
	local ray = Ray.new(unitray.Origin, unitray.Direction * 200)
	local target, position = workspace:FindPartOnRay(ray, game.Players.LocalPlayer.Character or nil)
	return position
end

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