Can someone help me out by script a goalkeeper?

I need to script a goalkeeper for my soccer (football) game.

What I need someone to teach me is how to align the goalkeeper (npc, dummy) with the ball.

For example, the ball is in the middle, the goalkeeper will also be in the middle of a selected region.
same for left, right, and anywhere. Something like the goalkeeper in the game “Super Striker League”:

robloxapp-20230108-1232593.wmv (1.6 MB)

3 Likes

One way would be to use the CFrame of the ball to determine where the goal keeper should be.


local function update()
    -- Get the goal keeper and make sure it is active.
    local gk = workspace.GoalKeeper
    if not gk or not gk.Parent then return end

    -- Get the ball's position and the goal keeper's position.
    -- Use the y-position of the ball to determine the location of the goal keeper.
    local ballPos = ball.Position
    local gkPos = gk.Position
    gkPos = Vector3.new(gkPos.X, ballPos.Y, gkPos.Z)

    -- Move the goal keeper to the calculated position.
    gk.CFrame = CFrame.new(gkPos)
end

-- Run the update loop every 0.1 seconds.
while true do
    update()
    wait(0.1)
end

https://developer.roblox.com/articles/CFrame-and-Positioning

2 Likes

local ballPos = ball.Position

I have a slight feeling this script was chatGPT generated, since you did not define “ball”, but I’ll edit it so that it works.

1 Like

No, ChatGPT would’ve probably provided a better answer.

1 Like

just because somethign wasn’t defined doesn’t automatically mean that its ChatGPT generated, but I also get the feeling that this was generated by ChatGPT purely because of the structure and comments.
Also, if you want it to be like Super Striker League, you’ll need a bounding box, which you can sort of create using math.clamp

1 Like

Yeah, and you can’t move a dummy using position I think, you have to move the rootpart.

I can set it to cancollide = off?

1 Like

set which CanCollide to off?
if you set the part’s CC to off, then the goalkeeper can walk through?
if you set the ball’s CC to off, then it’ll just fall through the floor…
if you set the player’s CC to off, it’ll also fall through the floor.
you’ll need to make use of collision filtering

1 Like

What do you mean the goalkeeper can walk through, that’s what I want. I want to goalkeeper to walk to align itself with the ball while inside a box, if it can’t walk through it’s stuck.

1 Like

When i say “part” i mean the bounding box around the goalkeeper that prevents him from just wandering around the field.

1 Like

Ohh, I get what you mean, only I don’t want it to look like he’s running into a wall. Have you watched the video I put above? It shows exactly what I’m trying to acheive and it doesn’t look like it’s running into a wall.

1 Like

Try detect when it is touching the wall, and when it is, it should switch to a sidestep animation towards the ball

1 Like
local NPC = script.Parent
local ball = workspace:WaitForChild("Ball")
local region = workspace:WaitForChild("Region")

while true do
	local ballPos = ball.Position
	local regionPos = region.Position

	if ballPos.Z < regionPos.Z - region.Size.Z/2 then
		NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, regionPos.Z - region.Size.Z/2)
	elseif ballPos.Z > regionPos.Z + region.Size.Z/2 then
		NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, regionPos.Z + region.Size.Z/2)
	else
		NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, ballPos.Z)
	end
	wait(0.05)
end

This script above worked, but the goalkeeper is rotated 90 degrees to the wrong side and I want to rotate it 90 degrees to the left, how do I do that?

I’ve tried doing it manually, but that won’t work.

1 Like

by “manually”, does that mean you tried applying an offset during the script? Also, “while true do” is not recommended (though I’m not sure if there’s an alternative - perhaps only run it if the ball is moving?) Just do an offset of of a few degrees, and print out the direction / offset to see if it works…

1 Like

Why don’t you check the balls position on a axis and then tween the goalkeeper to the axis. In order to see if the ball is close you can check the magnitude and see if its greater than the maximum distance or not. In order to stop the goal keeper from leaving his net you can set a axis limit on the axis and see if the balls axis is greater than the maximum axis and of it is you can tween the goalkeeper to the maximum axis.

2 Likes