Not tweening to the lookVector

Hello! So I’m trying to make an Asteroid script but for some reason the asteroids only land in one direction, the lookVector just doesn’t update. Code:

--MODULE SCRIPT

local gravity = {}

function gravity.Asteroid(player, Asteroid)
	--20 studs above the player head
	--50 studs in front of the player, tween it
	local TS = game:GetService("TweenService")
	local ti = TweenInfo.new(3)
	
     local goals = {Position = player.Character.PrimaryPart.CFrame.LookVector * 30}
	--Make it above the player's head
	--clone it
	local function update()
		while wait(1) do
			local goals = {Position = player.Character.PrimaryPart.CFrame.LookVector * 30}
		end
	end
	spawn(update)
	local clone = Asteroid:Clone()
	local tween = TS:Create(clone, ti, goals)
	clone.Parent = workspace
	local x = 0; local y = 50; local z = 0
	clone.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(x, y, z)
	--Now tween it 
	tween:Play()
	print("tween played")
	tween.Completed:Connect(function()
		print("Ended")
		wait(3)
		clone:Destroy()
	end)
end

return gravity

--LOCAL SCRIPT

local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		--Fire the event
		local replicatedStorage = game:GetService("ReplicatedStorage")
		local playerClicked = replicatedStorage:FindFirstChild("PlayerClicked")
		print("Invoked")
		local invoke = playerClicked:InvokeServer()
	end
end)

--SERVER SCRIPT

local replicatedStorage = game:GetService("ReplicatedStorage")
local playerClicked = replicatedStorage:FindFirstChild("PlayerClicked")
local gravity = require(script.Parent.Gravity)
local asteroid = game.ReplicatedStorage.Asteroid

playerClicked.OnServerInvoke = (function(player)
	--require the gravity
	--(player, Asteroid)
	gravity.Asteroid(player, asteroid)
	print("ServerEvent")
	return true
end)

U already defined goals up there. The other goals you define gets garbage collected since it’s just in the scope of the while loop. So you’re just creating a new goals variable every second but not using it.
Remove the local and just redeclare the initial goals variable like this:

while wait(1) do
	goals = {Position = player.Character.PrimaryPart.CFrame.LookVector * 30}
end

I would also recommend using coroutine.wrap(update)() instead of spawn(update) since spawn delays the function for some time.

Also, I haven’t read the whole code

1 Like

It’s better but it still doesn’t land infront of the player all the time.
Code:

--MODULE SCRIPT

local gravity = {}

function gravity.Asteroid(player, Asteroid)
	--20 studs above the player head
	--50 studs in front of the player, tween it
	local TS = game:GetService("TweenService")
	local ti = TweenInfo.new(3)
	goals = {Position = player.Character.PrimaryPart.CFrame.LookVector * 30}
	local function update()
		while wait(1) do
			goals = {Position = player.Character.PrimaryPart.CFrame.LookVector * 30}
		end
	end
	spawn(update)
	local clone = Asteroid:Clone()
	local tween = TS:Create(clone, ti, goals)
	clone.Parent = workspace
	local x = 0; local y = 50; local z = 0
	clone.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(x, y, z)
	--Now tween it 
	tween:Play()
	print("tween played")
	tween.Completed:Connect(function()
		print("Ended")
		wait(3)
		clone:Destroy()
	end)
end

return gravity

1 Like

I still need help. :confused:

30chars

A) You have a function inside a function “update” which you call with spawn.
B) You define “goals” twice without localising it even once.
C) You’re spawning in a new thread everytime you press the KeyCode you have in UIS probably causing immense lag and causing your script to just timeout.

Fix those 3 issues and your problem should be fixed.

1 Like