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

How can i fix my script? Inputservice and parent errors

Code:

ocal player = game.Players.LocalPlayer

local userInputService = game:GetService("UserInputService")

local animationID = "rbxassetid://5149640147"

local animPlaying = false

function crouch()
	if player:FindFirstChild("Contestant") then
		if animPlaying == false then
			local animObj = Instance.new("Animation")
			animObj.AnimationId = animationID
			local loadedAnim = player.Character.Humanoid:LoadAnimation(animObj)
			loadedAnim:Play()
			player.Character.Humanoid.WalkSpeed = 10
			animPlaying = true
			script.Parent.Crouch.ImageColor3 = Color3.fromRGB(26, 144, 255)
			script.Parent.Crouch.TextLabel.BackgroundColor3 = Color3.fromRGB(26, 144, 255)
			print("Playing")
		else
			print("Stopping")
			for i , v in pairs(player.Character.Humanoid:GetPlayingAnimationTracks()) do
				v:Stop()
			end
			player.Character.Humanoid.WalkSpeed = 16
			animPlaying = false
			script.Parent.Crouch.ImageColor3 = Color3.fromRGB(255,255,255)
			script.Parent.Crouch.TextLabel.BackgroundColor3 = Color3.fromRGB(255,255,255)
		end
	end
end

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input,gpe)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		print("Correct input")
		crouch()
	end
end)

script.Parent.Crouch.MouseButton1Click:Connect(function()
	crouch()
end)

game.ReplicatedStorage.ToggleCrouch.OnClientEvent:Connect(function(visiblity)
	script.Parent.Crouch.Visible = visiblity
	for i , v in pairs(player.Character.Humanoid:GetPlayingAnimationTracks()) do
		v:Stop()
	end
	player.Character.Humanoid.WalkSpeed = 16
	animPlaying = false
	script.Parent.Crouch.ImageColor3 = Color3.fromRGB(255,255,255)
	script.Parent.Crouch.TextLabel.BackgroundColor3 = Color3.fromRGB(255,255,255)
end)

Error:

15:03:17.629 - local player = game.Players.LocalPlayer
15:03:17.631 - 
15:03:17.632 - local userInputService = game:GetService("UserInputService")
15:03:17.632 - 
15:03:17.633 - local animationID = "rbxassetid://5149640147"
15:03:17.634 - 
15:03:17.635 - local animPlaying = false
15:03:17.635 - 
15:03:17.636 - function crouch()
15:03:17.636 - 	if player:FindFirstChild("Contestant") then
15:03:17.637 - 		if animPlayin:43: attempt to index nil with 'Parent'

Please help to improve this code! also here is the line where the code is being affected at!

Affected code:

script.Parent.Crouch.MouseButton1Click:Connect(function()
	crouch()
end)

1 answer

Ok so I made this an answer instead of comment since but in the beginning. The local is wrong i didnt see it so forgive me

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