#Problem with ball scripting
1 messages · Page 1 of 1 (latest)
LocalScript HasBall:
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local player = Players.LocalPlayer
local wrappedBall = workspace:WaitForChild("WrappedBall")
local claimEvent = ReplicatedStorage.Events:WaitForChild("ClaimBall")
local loseEvent = ReplicatedStorage.Events:WaitForChild("LoseBall")
local hasBall = player.BallMovement.HasBall
local distance = player.BallMovement.Distance
hasBall.Value = false
local canGrabBall = true
local function getCharacter()
local character = player.Character or player.CharacterAdded:Wait()
return character, character:WaitForChild("HumanoidRootPart")
end
local character, rootPart = getCharacter()
player.CharacterAdded:Connect(function()
character, rootPart = getCharacter()
end)
RunService.Heartbeat:Connect(function()
if not hasBall and rootPart then
local dist = (rootPart.Position - wrappedBall.Position).Magnitude
distance.Value = dist
if dist <= 7 then
hasBall.Value = true
claimEvent:FireServer()
else
hasBall.Value = false
end
end
end)
RunService.RenderStepped:Connect(function()
if hasBall and rootPart then
local targetCF = rootPart.CFrame * CFrame.new(0, -2, -3)
wrappedBall.CFrame = targetCF
end
end)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.E and hasBall then
loseEvent:FireServer()
canGrabBall = false
task.delay(1, function()
canGrabBall = true
end)
end
end)
Server Script Dribbling:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local wrappedBall = workspace:WaitForChild("WrappedBall")
local currentOwner = nil
local claimEvent = ReplicatedStorage.Events:WaitForChild("ClaimBall")
local loseEvent = ReplicatedStorage.Events:WaitForChild("LoseBall")
local selectionBoxes = {}
local function updateSelectionBox(player, enabled)
local char = player.Character
if not char or not char:FindFirstChild("HumanoidRootPart") then return end
if enabled then
if not selectionBoxes[player] then
local box = Instance.new("SelectionBox")
box.Adornee = char.HumanoidRootPart
box.LineThickness = 0.05
box.SurfaceTransparency = 1
box.Color3 = Color3.fromRGB(0, 162, 255)
box.Parent = char.HumanoidRootPart
selectionBoxes[player] = box
end
else
if selectionBoxes[player] then
selectionBoxes[player]:Destroy()
selectionBoxes[player] = nil
end
end
end
claimEvent.OnServerEvent:Connect(function(player)
if not currentOwner then
currentOwner = player
updateSelectionBox(player, true)
wrappedBall.CanCollide = false
end
end)
loseEvent.OnServerEvent:Connect(function(player)
if currentOwner == player then
updateSelectionBox(player, false)
currentOwner = nil
wrappedBall.CanCollide = true
end
end)
RunService.Heartbeat:Connect(function()
if currentOwner then
local char = currentOwner.Character
if char and char:FindFirstChild("HumanoidRootPart") then
wrappedBall.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0, -3, -2)
end
end
end)
serverscript passing:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local powerEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("Passing")
local ball = game:GetService("Workspace").WrappedBall
print("Server script is running and waiting for event...")
powerEvent.OnServerEvent:Connect(function(player, power, direction)
task.wait(0.1)
print("Passing event received from", player.Name, "with power:", power)
if not ball then return end
if typeof(direction) ~= "Vector3" then return end
power = math.clamp(power, 0, 100)
direction = Vector3.new(direction.X, 0, direction.Z)
direction = direction.Unit
local velocity = direction * power
local arcPower = 20
ball.AssemblyLinearVelocity = velocity
task.wait(0.05)
ball.AssemblyLinearVelocity = Vector3.new(velocity.X, arcPower, velocity.Z)
print("Setting ball velocity to:", ball.AssemblyLinearVelocity)
print(player.Name .. " passed with power:", power, "and direction:", direction)
end)
Local script Passing:
part 1
local player = game:GetService("Players").LocalPlayer
local replicatedStorage = game:GetService("ReplicatedStorage")
local userInputService = game:GetService("UserInputService")
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local power = player:WaitForChild("BallMovement"):WaitForChild("Power")
local passingEvent = replicatedStorage:WaitForChild("Events").Passing
local hasBall = player:WaitForChild("BallMovement"):WaitForChild("HasBall")
local passingOne = replicatedStorage:WaitForChild("Animations").PassingOne
local passingTwo = replicatedStorage:WaitForChild("Animations").PassingTwo
local passingThree = replicatedStorage:WaitForChild("Animations").PassingThree
local passingOneTrack = animator:LoadAnimation(passingOne)
local passingTwoTrack = animator:LoadAnimation(passingTwo)
local passingThreeTrack = animator:LoadAnimation(passingThree)
local loseBallEvent = game:GetService("ReplicatedStorage").Events:WaitForChild("LoseBall")
local lostBallEvent = game:GetService("ReplicatedStorage").Events:WaitForChild("LostBall")
local mouse = player:GetMouse()
local keybind = Enum.KeyCode.E
local poweradd = 1
local maxpower = 100
local holding = false
local originalWalkSpeed = humanoid.WalkSpeed
local arrowModel = nil
local passTip = nil
local greenColor = Color3.fromRGB(0, 255, 0)
local redColor = Color3.fromRGB(255, 0, 0)
part2
userInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == keybind and not holding then
holding = true
power.Value = 0
arrowModel = replicatedStorage:WaitForChild("PassArrow"):Clone()
arrowModel.Name = "ActiveArrow"
arrowModel.Parent = workspace
arrowModel.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(0, 2, -3)
passTip = Instance.new("Part")
passTip.Size = Vector3.new(0.2, 0.2, 0.2)
passTip.Anchored = true
passTip.CanCollide = false
passTip.Transparency = 1
passTip.Name = "PassTip"
passTip.Parent = workspace
while holding and power.Value < maxpower do
power.Value = math.min(power.Value + poweradd, maxpower)
if arrowModel and arrowModel.Parent and passTip and passTip.Parent then
local rootPos = character.HumanoidRootPart.Position
local mousePos = mouse.Hit.Position
local flatRootPos = Vector3.new(rootPos.X, 0, rootPos.Z)
local flatMousePos = Vector3.new(mousePos.X, 0, mousePos.Z)
local direction = (flatMousePos - flatRootPos).Unit
passTip.Position = rootPos + direction * 5 + Vector3.new(0, 2, 0)
arrowModel.CFrame = CFrame.new(rootPos + Vector3.new(0, 2, 0), passTip.Position)
local t = power.Value / maxpower
local lerpedColor = greenColor:Lerp(redColor, t)
arrowModel.Color = lerpedColor
end
task.wait(0.01)
end
end
end)
uhh it doesnt let me post the last part
Its a lot of scripts and the last part I cant post... I have a problem I cant fix for a long time ugh
you probably dont need the last part its just the thing that fires remote event for passing and actives animations.
My problem is that when Im dribbling everything works until I cant pass. its because for some reason the hasBall value is set to false even tho its supposed to be set to true. its set to false because the distance value doesnt work. All of this is in the hasBall script. Also, if I set the hasBall value to True manually, the ball doesnt move because its locked when In dribbling so I also have to make it so that when passing the ball isnt locked to my feet and I just didnt find out how. Any help please?
Scroll up for all the scripts that you might need.