Coordinates for missiles not exploding

A project I have been working has run into an error where if two different coordinates are given, any pre-existing missiles in the game would basically turn from their target to the new coordinates. If done enough, they will just sit on the ground of the game and remain there until a new coordinate is given, if close enough, finally exploding and fixing itself. I tried to rewrite the code where if the tool is equipped, it will remove the tool and you can no longer give any coordinates until you get another, which didn’t work, so I just removed it for now. The problem with that is that it is not affected by only the player’s action. If two people have this tool and managed to click two different points, the missiles would break, but no exact error would be given in the outputs.

TLDR: Missiles for a game breaking because two different coordinates are given.

I’m guessing that the error comes from the server script as it handles the information and movement of the missiles.

Image of the issue:
Issue here.

local ob = script.Parent
local rs = game:GetService("RunService")
local event = game.ReplicatedStorage.Events

local explodedistance = 10
local studs = 200
ob.loopthis:Play()

ob.BodyForce.Force = Vector3.new(0 ,ob:GetMass() * workspace.Gravity, 0)

event.GiveCords.OnServerEvent:Connect(function(plr, cords)
	local pos = cords
	local function moveto(steps)
		local distance = (pos - ob.Position).Magnitude
		if distance > explodedistance then
			ob.CFrame = ob.CFrame:Lerp(CFrame.new(pos), steps * studs/distance)
			ob.CFrame = CFrame.new(ob.Position, pos) * CFrame.Angles(math.rad(180), math.rad(0), math.rad(0))
		elseif distance < explodedistance then
			ob.explosion:Play()
			ob.loopthis:Stop()
			local exe = Instance.new("Explosion", workspace)
			exe.Position = ob.Position
			exe.BlastRadius = 100
			exe.BlastPressure = 900000
			ob:Destroy()
		end
	end

	rs.Stepped:Connect(function(time,steps)
		moveto(steps)
	end)
end)

1 Like