Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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)
0
Probably to prevent the common error of `Character (a nil value)`. Just simply do `Character = Player.Character or Player.CharacterAdded:Wait()` TheeDeathCaster 2368 — 6y
0
Because the player didn't load greatneil80 2647 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago

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 :)

0
Thank you for responding! DeadlyEra 2 — 6y
Ad

Answer this question