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

attempt to index local "ball". Even though the ball is in the character?

What im trying to do is making the basketball shoot or in other terms making the part go to where the person is looking at when he clicks down. Keep in mind the basketball is a part and not a tool.

local function Shoot_Event()
	local ball = Character:WaitForChild("Ball")
	if ball then
	local Hit = Mouse.Hit.p
	local ShootingPosition = Character.PrimaryPart.Position
	local HeadLook = (Hit - Character.Head.Position).unit
	local SpawnPosition = ShootingPosition + HeadLook * 5
	local Power = ball.Power.Value
	print("work")
	ShootingEvent:FireServer(Hit, SpawnPosition, Power)
	end 
end

this local script works fine

local function Server_Shoot(Player, Hit, SpawnPos, Power)
	local Character = Player.Character or game.Workspace:FindFirstChild(Player.Name)
	local ball = Character:FindFirstChild("Ball")
		ball.Parent = game.Workspace
		ball.Position = SpawnPos
		ball.LastPlayer.Value = Player
		ball.Velocity = (Player.Character.Head.Position - Hit).unit * - Power + Vector3.new(0,50,0)
	end

But however this server script doesn't for some reason it says "attempt to index local "ball" (a nil value)

6 comments

0

Is ball being used in just that function?

0

where was the ball made?

0

The ball is being used in different functions. The ball is a part and not a tool but the error is on 4th line

0

in the first script u need to do local ball = Character:FindFirstChild("ball") because wait is used on something thats always inside from the start of spawn

0

so do not use WaitForChild because one example is WaitForChild can be used on Humanoid or HumanoidRootPart etc

0

I did i used FindFirstChild() but then it didn't work either i printed the ball's parent and it was the characters model

1 answer

Based on the differences between the local and server scripts, the ball probably doesn't exist quite yet when it's being looked for in the server script. The LocalScript uses WaitForChild() while the Script uses FindFirstChild(). Try switching FindFirstChild() to WaitForChild() in the server script and see if that helps.

1 comment

0

Entirely possible. From my own experiences with this issue, it's either this right here or that the ball is being made in a localscript. If the ball is made on the client, only the localscripts will be able to see it, and you would get that error while trying to access it on the server.

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