Game saying that a vector force does not exist

I have a script that clones a ball and welds it to the player but when I try to define the vector force that is already a part of the ball model a I clone it says it does not exist
the error message is: Players.TheGameMaker178.PlayerScripts.LocalScript:10: attempt to index nil with ‘VectorForce’

localScript:

local Player = game.Players.LocalPlayer
local RequestBall = game.ReplicatedStorage.RemoteFunction
local CAS = game:GetService("ContextActionService") 
local Ball
local Humanoid
local VectorForce

Player.CharacterAdded:Connect(function(Char)
	Ball = RequestBall:InvokeServer()
	VectorForce = Ball:WaitForChild("VectorForce")
	Humanoid = Char:WaitForChild("Humanoid")
end)

local function APressed(actionName, inputState, inputObject)
	if actionName == "PRESS_W" then
		if inputState == Enum.UserInputState.Begin then
			local MoveDirection = Humanoid.MoveDirection
			local NewForce = MoveDirection * 32
			VectorForce.Force = Vector3.new(NewForce)
		end
	end
	
	
end

CAS:BindAction("PRESS_W", APressed, false, Enum.KeyCode.W)
local RemoteFunction = game.ReplicatedStorage.RemoteFunction
local CBall = game.ReplicatedStorage.Ball:Clone()

function RemoteFunction.OnServerInvoke(Player)
	if Player.Character then
	local Char = Player.Character
	local random = Random.new()
	local HRT = Char.HumanoidRootPart
	local Humanoid = Char.Humanoid
		
	local Ball = CBall:Clone()
	Ball.Shape = Enum.PartType.Ball
	Ball.Size = Vector3.new(7.5, 7.5, 7.5)
	Ball.Name = "Marble"
	Ball.BrickColor = BrickColor.random()
	Ball.Anchored = false
	Ball.Transparency = 0.5
	Ball.Material = Enum.Material.Plastic
	Ball.Parent = Char
	Ball.CFrame = HRT.CFrame
		
	local Weld = Instance.new("WeldConstraint")
	Weld.Part0 = Ball
	Weld.Part1 = HRT
	Weld.Parent = Ball
	
	local VectorForce = Instance.new("VectorForce")
	VectorForce.Enabled = true
	VectorForce.ApplyAtCenterOfMass = true
	VectorForce.Parent = Ball
		
	HRT.Anchored = false
	Humanoid.PlatformStand = true
	
	return Ball
   end
end
1 Like

It’s not saying that the vector force doesn’t exist. It’s saying that the ball doesn’t exist. If you try printing out Ball just after the :InvokeServer, does it actually exist?

Yes Ik. But I am returning the ball I cloned in this function so why would it be nil?

because for the client to see something created by the server through remote event/function, it must exist both in server and client, such as workspace, it must exist in workspace so the client can see it, so when you send the item through remote event to the client, the server is not sending the Instance itself, it is sending the uniqueid of the instance, so the client can locate the object in explorer.

in summary, u can never send Instances through remote events/functions, only the uniqueid will be sent, and that is also only if it exists both in server or client (workspace, replicatedstorage), and if possible, give the client some time to load the item in workspace first before you pass the item

if it still doesnt work, try making bunch of debugging prints to know if the remote function even gets fired in the first place

The problem is that on the client the ball is nil.
I’ve tested it by giving it time to load before runnning the waitforchild but it still errors

does the remote function actually getting fired?

yes I’ve checked usng print statements

when I parented the ball to the workspace instead of the player it worked

yea you did not give the client some time to load the ball, i added this below the Ball = RequestBall

	repeat task.wait() until Ball ~= nil

and no longer error about the ball being nil, BUT now theres another error after that saying Humanoid is nil, so im trying to find out why

oh i found out, you used “Char” as parameter in characteradded function, its better practice to do

repeat task.wait() until Player.Character ~= nil
local Char = Player.Character

If workspace.StreamingEnabled is true, returning a new instance with a RemoteFunction doesn’t guarantee it will exist on the client immediately. Instead of Ball = RequestBall:InvokeServer() you should wait for the ball to exist with WaitForChild. This is straightforward in your case since the ball is parented to the character.

RequestBall:InvokeServer()
Ball = Char:WaitForChild("Marble") -- you could also use ChildAdded if you want

If every character in your game gets a ball, you could avoid using a RemoteFunction entirely by listening to CharacterAdded on the server instead of on the client. You can also just disable streaming if you want instances returned by RemoteFunctions to be instantly replicated. Hope this helps

This worked. So the problem was that The ball did not have enough time to replicate?

Yes. The replication delay with streaming is documented here:

1 Like

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