Attempt to index nil with 'Character'

Script is in proximity prompt in a part in workspace
When the prompt is triggered I keep getting this error
Workspace.Trees.Tree.Trunk.ProximityPrompt.Script:2: attempt to index nil with ‘Character’

local function onPromptTriggered(promptObject, player)
	local character = player.Character
	if character then
		local humanoid = character:WaitForChild("Humanoid")
		local animation = game:GetService("InsertService"):LoadAsset(humanoid.Animations.SideSwipe) 
		local animationTrack = humanoid:LoadAnimation(animation)
		animationTrack:Play()

		animationTrack.Stopped:Connect(function()
			animation:Destroy()
		end)
	end
end

script.Parent.Triggered:Connect(onPromptTriggered)

The error is being caused by either

  1. The player being nil
  2. The player and character haven’t loaded in yet by the time the script fires its signal

You should replace the 2nd line with

local character = player.Character or player.CharacterAdded:Wait()

Also, remember that server scripts run on the server, so they can’t access the local player, which is on the client.

2 Likes

The Triggered event only passes the player through. Remove the promptObject variable in the function and it should work.

local function onPromptTriggered(player)
	local character = player.Character
	if character then
		local humanoid = character:WaitForChild("Humanoid")
		local animation = game:GetService("InsertService"):LoadAsset(humanoid.Animations.SideSwipe) 
		local animationTrack = humanoid:LoadAnimation(animation)
		animationTrack:Play()

		animationTrack.Stopped:Connect(function()
			animation:Destroy()
		end)
	end
end

script.Parent.Triggered:Connect(onPromptTriggered)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.