Defining Character Bug

Below is code that I have made to get the character from the player that kicked a ball, the only works after the first time the ball is kicked (it doesn’t work the first time and gives this error "attempt to index nil with ‘Character’ ". I have no idea why the code only works the second time around.

Trigger.Changed:Connect(function()
	local kicker = script.Parent.Kicked.Value
	local plr = game.Players:FindFirstChild(kicker)
	local Char = plr.Character
		local mode = Char:FindFirstChild("Mode")
		local punch = Char.Punch.Value 
		local HRP = Char.HumanoidRootPart
		ball:SetNetworkOwner(plr) 
		ball.Parent = workspace 
end)

You could try changing

local Char = plr.Character

to

local Char = plr.Character or plr.CharacterAdded:Wait()

Where and when does the Kicked value get set?
It seems to be trying to get a player from the Kicked value, but there may be no player set when that line is run, hence the “attempt to index nil with ‘Character’” error, but when running the code a second time, the Kicked value had been already set to the player previously, so the ball script works without issues.

tl;dr: I think the Kicked value is set after Trigger is activated.

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