"..." Is not a member of workspace - How can I fix this?

Most people have probably had this error before, but if I try to call something from the workspace it will run the script before the workspace.
Right now I have a wait() at the start of my code, but that only stops it sometimes.
I’ve tried game.Players.LocalPlayer.CharacterAdded:Wait(), but that doesn’t ever run anything after that.
Is there any way to fix this?

You can try this, always work for me.

local plr = game.Players.LocalPlayer
local char = plr.Character
repeat task.wait(); char = plr.Character until plr.Character

The player’s character might’ve loaded before the script began, making this wait until the NEXT player respawn. Use Tobi’s answer above or alternatively use something like this

local function onCharacter()
	-- ...
end

if player.Character then
	onCharacter(player.Character)
end
player.CharacterAdded:Connect(onCharacter)

Actually it’s better to use both.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
4 Likes

What’s the parent of the script?

Correct me if I’m wrong or misunderstood what you are asking, but couldn’t you just yield the script until the part in the workspace is loaded using WaitForChild?

local partToCall = game.Workspace:WaitForChild("PartName")

--code relating to the part

that’ll error with infinite yield on waitforchild partname, you need to specify a timeout

workspace:WaitForChild(“PartName”,10)

10 relates to the number of seconds to keep trying to wait for it to be added

Ah yes, I knew I was forgetting something. Although using that would still work for OP

The marked solution is probably not the best option btw

local plr = game:GetService(“Players”).LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
print(character)