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
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?
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
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