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

Why does it say ca is a nil value when it is not? is this a bug

Asked by 4 years ago
Edited 4 years ago
local p = game.Players.LocalPlayer
p.CharacterAdded:Connect(function(ch)
    ca = p.Character
end)
local m = p:GetMouse()
local UIS = game:GetService("UserInputService")
ca.Humanoid.WalkSpeed = 25



attempt to index a nil value, line 8

1 answer

Log in to vote
2
Answered by
Elixcore 1337 Moderation Voter
4 years ago

CharacterAdded fires in the same tick as the character begins to exist.adding everything to it and setting it as the player's character will not happen when the event is called.

I recommend instead of that, doing this.

local p = game.Players.LocalPlayer
local ca = p.Character or p.CharacterAdded:Wait()

local m = p:GetMouse()
local UIS = game:GetService("UserInputService")
ca.Humanoid.WalkSpeed = 25

ca will be the character no matter what, as it will wait for it to get added if it needs to.

Ad

Answer this question