Basically I’m trying to make a basketball shooting system, and I used EgoMoose’s tutorial, but I wanted to change a couple things:
- I only want the ball to be able to be shot while the ball is in the player’s hands.
- Instead of cloning the ball, I want the tool to be dropped, while the handle is still shooting.
I’m having problems accomplishing this, here’s my code:
Local Script:
script.Parent.Equipped:Connect(function()
local t = 1;
local mouse = game.Players.LocalPlayer:GetMouse();
local hrp = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("HumanoidRootPart");
local bball = script.Parent:WaitForChild("Handle");
mouse.Button1Down:Connect(function()
local g = Vector3.new(0, -game.Workspace.Gravity, 0);
local x0 = hrp.CFrame * Vector3.new(0, 2, -2)
-- calculate the v0 needed to reach mouse.Hit.p
local v0 = (mouse.Hit.p - x0 - 0.5*g*t*t)/t;
local b = game.Players.LocalPlayer:WaitForChild("Backpack"):FindFirstChild("Basketball")
game.ReplicatedStorage.Shooting:FireServer(v0, x0, b)
end)
end)
Server Script:
game.ReplicatedStorage.Shooting.OnServerEvent:Connect(function(player, v0, x0, b)
local h = b:FindFirstChild("Handle")
h.Velocity = v0;
h.CFrame = CFrame.new(x0);
h.CanCollide = true;
b.Parent = game.Workspace;
end)
I get an error that says: ServerScriptService.Script:2: attempt to index nil with 'FindFirstChild'
, but I’m not sure how I’m trying to find a child in something that doesn’t exist. How can I accomplish my goal and eliminate the errors?
A solution I tried was adding a wait in the server script, and while it doesn’t throw any errors, nothing is happening.