Why does this value return nil?

Hello there, I am currently trying to make a vehicle spawner for my game. I have started off with creating a LocalScript finding the character of the player in the workspace. I got that in a value. Now whenever I try to make use of that value, it returns nil.

I have to make sure that the vehicle I clone with that script does not run in the client.

I don’t see why it’s returning nil however you might as well use the LocalPlayer’s Character, as long as that doesn’t present any other issues:

Clone:MoveTo(game:GetService("Players").LocalPlayer.Character.HumanoidRootPart.Position)

u cant call script.Parent.Spawner.Value.HumanoidRootPart.Position

u should use:

local char = game.Workspace:FindFirstChild(script.Parent.Spawner.Value)
if char then
local Torso = char.Torso or char.HumanoidRootPart
if Torso then
Clone:MoveTo(Torso.CFrame)

  end
end

Instead of CFrame u can use Position

Use game.Players.LocalPlayer.Character to find the player character instead.

This “Spawner” value already is an ObjectValue, and consists of the right character already.

Still returns nil.

script.Parent.Spawner.Value = game.Players.LocalPlayer.Character

^ In a LocalScript.

instead of MoveTo try using Lerp

no, Lerp dont work use :TranslateBy

You shouldn’t be using a non-local script in PlayerGui. The local script updating the spawn value won’t update it for the server script, and the server script probably won’t even run properly.

Oh I just noticed you’re setting the Spawner value in a local script, but then try to access it from the server. The server will not see changes you make in a local script.

Also as mentioned, don’t put server scripts in the player gui. They don’t go there and you shouldnt rely on it working.

2 Likes

Just use:

Clone:MoveTo(game.Players.LocalPlayer.Character.HumanoidRootPart.Position)

I don’t see why the value is nil though, probably because you call it before the value is set

Edit: @madattak is right, the server will not replicate any changes done on the client, and normal scripts should not be in PlayerGui.

The server script should be placed in ServerScriptService and you will have a RemoteEvent. The server script will handle the RemoteEvent with OnServerEvent and will create and teleport the car to the player. In the local script, when the player presses the GUI button, you should fire that event to the server

1 Like

I now see what the problem is, but I think that I am not far enough with my programming skills for all that RemoteEvent stuff, thanks for your help anyway.

You can read this to try and understand events Custom Events and Callbacks | Documentation - Roblox Creator Hub