Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Cannot find character, eventhough the character is there?

So I was messing about in Voidacity's Script Builder, trying to make a script

Player = game.Players.tantec
Char = Player.Character
parts = {}
parts2 = {}

coroutine.resume(coroutine.create(function()
	for i = 1,math.huge do
		part = Instance.new("Part", Char)
		part.Size = Vector3.new(1.2,1.2,1.2)
		part.Material = "Neon"
		part.Anchored = true
		part.CanCollide = false
		part.TopSurface = "Smooth"
		part.BottomSurface = "Smooth"
		part.Color = Color3.new(1,1,1)
		part.CFrame = Char["Right Arm"].CFrame * CFrame.new(0,-1,0) * CFrame.Angles(math.rad(math.random(-180,180)), math.rad(math.random(-180,180)), math.rad(math.random(-180,180)))
		parts[i] = part
		coroutine.resume(coroutine.create(function()
			for a = 1,5 do
				parts[i].Transparency = parts[i].Transparency + 0.2
				wait()
			end
			parts[i]:Destroy()
		end))
		wait()
	end
end))
coroutine.resume(coroutine.create(function()
	for i = 1,math.huge do
		part = Instance.new("Part", Char)
		part.Size = Vector3.new(1.2,1.2,1.2)
		part.Material = "Neon"
		part.Anchored = true
		part.CanCollide = false
		part.TopSurface = "Smooth"
		part.BottomSurface = "Smooth"
		part.Color = Color3.new(1,1,1)
		part.CFrame = Char["Left Arm"].CFrame * CFrame.new(0,-1,0) * CFrame.Angles(math.rad(math.random(-180,180)), math.rad(math.random(-180,180)), math.rad(math.random(-180,180)))
		parts2[i] = part
		coroutine.resume(coroutine.create(function()
			for a = 1,5 do
				parts2[i].Transparency = parts2[i].Transparency + 0.2
				wait()
			end
			parts2[i]:Destroy()
		end))
		wait()
	end
end))


It says 'Line 8, Character is not a valid member of Player', I was wondering why it was saying this, because I did have a character?

4 comments

0

Your script will throw an error if you don't join the game, you might want to fix it

0

void script builder is dying. but good luck

0

use local variables & dont use 2nd parent of instance.new

0

Don't use the second parameter of instance.new to parent things, as green271 said. This is because it's laggier than using the .Parent method.

3 answers

The character of a player does not load in immediately when the game starts. This means that your Char reference will be nil when the coroutine starts. In order to fix this problem, wait for the CharacterAdded event to fire or use the character if it's already loaded in like so:

Player = game.Players.tantec
Char = Player.Character or Player.CharacterAdded:Wait()

you could try something like using conditional statements to see if the player is named "tantec" or try the WaitForChild() function.

I think the problem you are having is that the script is running before the player object and the player character are in the game which leads to the plr variable and the character variable being nil.

Conditional statements method

game.Players.PlayerAdded:Connect(function(plr)
	if plr.Name == "tantec" then
		plr.CharacterAdded:Connect(function(char)
			...
		end)
	end
end)

--or possibly... (I never tried this so do at your own risk)

local plr = game.Players.PlayerAdded:Wait()
local char
if plr.Name == "tantec" then
	char = plr.Character or plr.CharacterAdded:Wait()
end

if char then
	...
end

WaitForChild method

local plr = game.Players:WaitForChild("tantec")
print("done waiting")
local char = plr.Character or plr.CharacterAdded:Wait()

...

To find the character, you could do

local Char = Player.Character or Player.CharacterAdded:Wait() -- If not found, it will wait for the character to be added.

-- Also, arguments after Instance.new() is depreacted. To prevent that, you can do

local h = Instance.new("Part")
h.Parent = workspace -- example

Hope this helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.