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

Getting a humanoid in a local script?

Hi I was making this script and when I saved it the players walkspeed didn't go to 0. Can you help?

game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
plr = game.Players.LocalPlayer
script.Parent.TextWrapped = true
plr.CameraMinZoomDistance = 1
plr.CameraMaxZoomDistance = 1
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 0
wait(5)
script.Parent.Text = "Hello " ..plr.Name.. ", and welcome to Conscious!"
wait(3)
script.Parent.Text = "You are probably wondering who I am..."
wait(3)
script.Parent.Text = "I am your Conscious telling you what to do and when to do it."
wait(3)
script.Parent.Text = "I am you."
wait(3)
script.Parent.Text = ""
plr.CameraMaxZoomDistance = 10
plr.CameraMinZoomDistance = 10
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
local a = game.ReplicatedStorage.Flashlight:Clone()
a.Parent = plr.Backpack
local b = game.ReplicatedStorage.Flashlight:Clone()
b.Parent = plr.StarterPack
wait(2)
script.Parent.Text = "To be continued..."

1 answer

Your problem is you're calling the player's character before it has a chance to load. Use a repeat loop or something that waits until the character exists. Personally, i like creating a variable that calls the player if it exists, and waits for it with the CharacterAdded event if it doesn't.

Try this:

local rep = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
repeat wait() until char:FindFirstChild("Humanoid")
local human = char.Humanoid

script.Parent.TextWrapped = true
player.CameraMinZoomDistance = 1
player.CameraMaxZoomDistance = 1

human.WalkSpeed = 0
wait(5)
script.Parent.Text = "Hello " ..player.Name.. ", and welcome to Conscious!"
wait(3)
script.Parent.Text = "You are probably wondering who I am..."
wait(3)
script.Parent.Text = "I am your Conscious telling you what to do and when to do it."
wait(3)
script.Parent.Text = "I am you."
wait(3)
script.Parent.Text = ""
player.CameraMaxZoomDistance = 10
player.CameraMinZoomDistance = 10
human.WalkSpeed = 16
local a = rep.Flashlight:Clone()
a.Parent = player.Backpack
local b = rep.Flashlight:Clone()
b.Parent = player.StarterPack
wait(2)
script.Parent.Text = "To be continued..."

Also, take advantage of using variables so you don't have to write lengthy lines of code over and over.

1 comment

0

Why don't you use player.Character:WaitForChild("Humanoid") instead of repeat wait() until char:FindFirstChild("Humanoid")?

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