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

[Actual Question] Why did I need to put a wait for this script?

Why would I need to put a wait(1) here for this script to work when you press a button? I'd appreciate if anyone could explain to me why I must put a wait(1)

wait(1)
local Player = game.Players.LocalPlayer
local Character = Player.Character

local Button = script.Parent

local BodyColors = Character:FindFirstChild("Body Colors")

Button.MouseButton1Click:Connect(function()
	BodyColors.HeadColor3 = Color3.new(1, 232/255, 220/255)
	BodyColors.LeftArmColor3 = Color3.new(1, 232/255, 220/255)
	BodyColors.LeftLegColor3 = Color3.new(1, 232/255, 220/255)
	BodyColors.RightArmColor3 = Color3.new(1, 232/255, 220/255)
	BodyColors.RightLegColor3 = Color3.new(1, 232/255, 220/255)
	BodyColors.TorsoColor3 = Color3.new(1, 232/255, 220/255)
end)

2 comments

0

Probably to prevent the common error of Character (a nil value). Just simply do Character = Player.Character or Player.CharacterAdded:Wait()

0

Because the player didn't load

1 answer

✓ Accepted answer

You actually don't need a wait there at all. The likely error is you're calling Player.Character too early as they may not even have been loaded in all the way. Best advice is this.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded
repeat wait() until Character

local Button = script.Parent

local BodyColors = Character:FindFirstChild("Body Colors")

Button.MouseButton1Click:Connect(function()
    BodyColors.HeadColor3 = Color3.new(1, 232/255, 220/255)
    BodyColors.LeftArmColor3 = Color3.new(1, 232/255, 220/255)
    BodyColors.LeftLegColor3 = Color3.new(1, 232/255, 220/255)
    BodyColors.RightArmColor3 = Color3.new(1, 232/255, 220/255)
    BodyColors.RightLegColor3 = Color3.new(1, 232/255, 220/255)
    BodyColors.TorsoColor3 = Color3.new(1, 232/255, 220/255)
end)

Hope this helps :)

1 comment

0

Thank you for responding!

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