[HELP!] How to make a goalkeeper

Hello. I am making a football game with a goalkeeper that would position itself in Z axis of the ball. Below is what I made. But there is an issue. I do not want goalkeeper to always stand next to ball and player to not be able to score. Basically I want goalkeeper to have reaction time.

local Gk = game.Workspace.Goalkeeper
local Ball = game.Workspace.Ball


while true do task.wait()
	Gk:MoveTo(Vector3.new(
		
		171.884,
		-38.478,
		Ball.Position.Z))
end
2 Likes

You can try adding a task.delay() to your script. This will help delay part of your function without throttling the script.

Like this?

while true do
	task.wait()
	task.delay(3, function()

		task.wait()
		Parent:MoveTo(Vector3.new(

			171.884,
			-38.478,
			Ball.Position.Z))

	end)
end

I think that should work. Try playtesting it.

It did not work. Maybe I should try tweening? Can you show me how?

make it wait a random delay

local Gk = game.Workspace.Goalkeeper
local Ball = game.Workspace.Ball


while true do
	task.wait(math.random()*1.5)
	Gk:MoveTo(Vector3.new(
		171.884,
		-38.478,
		Ball.Position.Z))
end

this will randomly wait between 0 and 1.5 seconds everytime the loops fires

I ended up using Tweens.

local Gk = game.Workspace.Goalkeeper
local Ball = game.Workspace.Ball
local goal = {
	CFrame = CFrame.new(171.884, -38.478, Ball.Position.Z) * CFrame.Angles(0,math.rad(90),0)
}--Set the destination for the part to move to.

local info = TweenInfo.new(
	0.5,                      --How long the tween is played for (in seconds)
	Enum.EasingStyle.Quad   ,   --The behavior of the tween
	Enum.EasingDirection.Out,   --The direction of the style
	0,                      --How many times the tween will be repeated
	false,                  --Will the tween play in reverse as well?
	0                   --Delay between each interpolation.
)

spawn(function()
	while true do
		wait(0.6)
		print(goal)

		
		local Tween = game:GetService("TweenService"):Create(Gk["Goalkeeper (Joe)"].Torso ,info,goal)

		Tween:Play()

	end
end)


spawn(function()
	while true do
		wait(1)
		goal = {
			CFrame = CFrame.new(171.884, -38.478, Ball.Position.Z) * CFrame.Angles(0,math.rad(90),0)
		}
	end

end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.