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

Why does my player scaling script print this error that shouldn't exist?

The error: Players.Player1.PlayerGui.ScreenGui.TextButton.LocalScript:9: attempt to index global 'chr' (a nil value)

The script:

button = script.Parent
chr = game.Players.LocalPlayer.Character
function monster()
		local n1 = math.random(100)		
		local n2 = math.random(100)	
		local n3 = math.random(100)	
		local av = n1 + n2 + n3 / 3		
		
		chr.Humanoid.JumpPower = av * 2
		chr.Humanoid.WalkSpeed = av
		chr.Humanoid.BodyDepthScale.Value = n1
		chr.Humanoid.BodyHeightScale.Value = n2
		chr.Humanoid.BodyWidthScale.Value = n3
end
button.MouseButton1Click:connect(monster)

1 comment

0

An error is for a reason, not just to troll you.

2 answers

You need to wait for your Character

I know that everyone else has already said this, but let's be honest - Their answers are wrong.

local plr  = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:wait()
-- Get the Character. If the Character is nil, wait for it.

Note that if this script is in PlayerScripts or some other non-resetting place, this will break after you die once. If this is the case, consider moving the chr declaration into the monster function body.

You need to wait for the character to load before getting it

repeat wait(.1) until game.Players.LocalPlayer.CharacterAdded

will yield until the character has spawned / been added

3 comments

0

That will always wait .1 seconds. Consider your condition.

0

That will always wait .1 seconds. Consider your condition.

0

That will always wait .1 seconds. Consider your condition.

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