How do i make it so when you touch a ball it follows the player

  1. What do you want to achieve? Keep it simple and clear!

Basically Want To Achieve This

How can i make it so the ball like follow the player.

5 Likes

you can weld it or you can heartbeat loop it to be in front of your character

4 Likes

heres what i got, it worked but it lags so bad

local distance = -2.5

local ball1 = game.Workspace:WaitForChild("Ball1")

local RunService = game:GetService("RunService")

ball1.Touched:Connect(function(Hit)
	local hrp = Hit.Parent:WaitForChild("HumanoidRootPart")
	local Follows = hrp
	while true do
		RunService.Heartbeat:Connect(function()
			ball1.CFrame = Follows.CFrame * CFrame.new(0 ,1 , distance)
			ball1.CFrame = CFrame.new(ball1.CFrame.X, 1, ball1.CFrame.Z)
			wait()
		end)
		wait()
	end
end)
2 Likes

is there a way for it not to lag

Try welding it instead.

ball.Touched:Connect(function(Hit)
	local hrp
	
	if Hit.Parent:FindFirstChild("HumanoidRootPart") then
		hrp = Hit.Parent.HumanoidRootPart
		if ball1:FindFirstChild("Weld") == nil then
			local weld = Instance.new("Weld")
			weld.Parent = ball1
			weld.Part0 = hrp
			weld.Part1 = ball1
			weld.C1 = CFrame.new(0 ,1 , distance)
		end
	end
end)
2 Likes

so if i kick the ball i need to destroy the weld?

Yep! The ServerEventFunc would be a good place to destroy the weld. Right before you calculate the balls movement.

This is a troll right :skull:
Aint no way… I wonder why it “Lags so bad”

2 Likes

It’s gotta be that pesky :WaitForChild() Am I right? :wink:

ok you guys got me yes i troll

1 Like

im dumb on this, it didnt work

local ServerEvent = game:GetService("ReplicatedStorage"):WaitForChild("ServerEvent")
local ball = game:GetService("Workspace"):WaitForChild("Ball")
local value = ball.Value
local distance = 4
local FootWeld

local function ServerEventFunc(player)
	
	FootWeld:Destroy()
	local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
	local forceDirection = (humanoidRootPart.Position - ball.Position).unit
	forceDirection = Vector3.new(forceDirection.x, value.Value, forceDirection.z)

	local forceMagnitude = -50

	ball.Velocity = forceDirection * forceMagnitude
end

ball.Touched:Connect(function(Hit)
	local hrp

	if Hit.Parent:FindFirstChild("HumanoidRootPart") then
		hrp = Hit.Parent.HumanoidRootPart
		if ball:FindFirstChild("Weld") == nil then
			local weld = Instance.new("Weld")
			weld.Parent = ball
			weld.Part0 = hrp
			weld.Part1 = ball
			weld.C1 = CFrame.new(0 ,2 , distance)
			FootWeld = weld
		end
	end
end)

ServerEvent.OnServerEvent:Connect(ServerEventFunc)

Yes, waitforchild is the only issue! there is no other issue with this that may cause memory leak!

Calling wait() at the end of RunService.Heartbeat is very usefull, you are yeilding absolutly nothing as this thread is running asynchronously…

Creating a new runservice.hreatbeat listener every iteration will definitly not cause any memory leak!

the first line of code here is very necessary! (it does literally nothing as its cframe is being compeltely overwritten in the second)

very good that you’re using wait(), shows you are up to dae with the latest coding practices!
Screenshot 2023-11-05 at 1.51.27 AM

Interesting, you are running all this code every touched event without any debounce, definitly will not cause any compounding memory leak as each time the ball is touched it will create multiple threads running loops that also create multiple events listening for ball touhced update!

Oh wait… looks like :waitforchild() is not the only issue

ok i kind of understand but can you help me with the weld cause im not familiar with it

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