Hi,
I was trying to make a sprint script with runnin’ animation but my issue is that it gives me an error. The error tells me that it doesn’t find the character, I know that I am trying to access the character before it is created. I am so confused.
Script:
local sonicAnimation = script.SonicAnimation
local UIS = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local char = player.Character
local hum = char.Humanoid
local camera = workspace.CurrentCamera
local sonicAnimationTrack = hum:LoadAnimation(sonicAnimation)
UIS.InputBegan:Connect(function(input)
if input == Enum.KeyCode.LeftShift then
sonicAnimationTrack:play()
for i = 1,16 do
wait()
camera.FieldOfView = camera.FieldOfView + 1.16
end
for i = 1,50 do
wait()
char:WaitForChild("Humanoid").WalkSpeed = char:WaitForChild("Humanoid").WalkSpeed + 2
end
end
end)
UIS.InputEnded:Connect(function(input)
if input == Enum.KeyCode.LeftShift then
sonicAnimationTrack:play()
for i = 1,16 do
wait()
camera.FieldOfView = camera.FieldOfView - 1.16
char:WaitForChild("Humanoid").WalkSpeed = char:WaitForChild("Humanoid").WalkSpeed - 2
end
end
end)
I changed your code to add a simple check. The easy way to do this would be with the character added event. (Which is fun because you can call it and get the character if player.Character is nil.)
local sonicAnimation = script.SonicAnimation
local UIS = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait();
local hum = char.Humanoid
local camera = workspace.CurrentCamera
local sonicAnimationTrack = hum:LoadAnimation(sonicAnimation)
UIS.InputBegan:Connect(function(input)
if input == Enum.KeyCode.LeftShift then
sonicAnimationTrack:play()
for i = 1,16 do
wait()
camera.FieldOfView = camera.FieldOfView + 1.16
end
for i = 1,50 do
wait()
char:WaitForChild("Humanoid").WalkSpeed = char:WaitForChild("Humanoid").WalkSpeed + 2
end
end
end)
UIS.InputEnded:Connect(function(input)
if input == Enum.KeyCode.LeftShift then
sonicAnimationTrack:play()
for i = 1,16 do
wait()
camera.FieldOfView = camera.FieldOfView - 1.16
char:WaitForChild("Humanoid").WalkSpeed = char:WaitForChild("Humanoid").WalkSpeed - 2
end
end
end)```
2 Likes
It worked, thank you (:. but it made the game run slower.
In what way did it make it run slower?
All it’s doing it referencing the players character, and if they don’t have one, wait until they do.