So I’m making a dodgeball where the player clicks it clones a ball and throws it to the direction where the mouse had click. Pretty similar to other projectiles. However, mines is not very accurate. It cant throw accurately. The ball curves to a different direction and most of the time its inches off. I’ve never programmed projectiles before so I’m not sure what I am doing.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local damageEvent = ReplicatedStorage:WaitForChild("DamageEvent")
local throwToolEvent = ReplicatedStorage:WaitForChild("ThrowToolEvent")
throwToolEvent.OnServerEvent:Connect(function(player, targetPosition)
local toolModel = ReplicatedStorage.Weapons:WaitForChild("Dodgeball")
local thrownTool = toolModel:Clone()
thrownTool.Parent = game.Workspace
local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
local handle = thrownTool:FindFirstChild("Handle")
if handle then
handle.Position = humanoidRootPart.Position + Vector3.new(1, 5, 0)
local throwDirection = (targetPosition - handle.Position).unit -- This line is probably the problem but not sure how to fix it.
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = throwDirection * 200
bodyVelocity.MaxForce = Vector3.new(8000, 8000, 8000)
bodyVelocity.Parent = handle
for _, part in ipairs(thrownTool:GetChildren()) do
if part:IsA("BasePart") then
part.CanCollide = true
end
end
local function onTouch(otherPart)
local character = otherPart.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
local otherPlayer = game.Players:GetPlayerFromCharacter(character)
if humanoid and character.Name ~= player.Character.Name then
local damageAmount = -15
humanoid:TakeDamage(15)
damageEvent:FireClient(player, damageAmount)
thrownTool:Destroy()
end
end
handle.Touched:Connect(onTouch)
wait(0.1)
bodyVelocity:Destroy()
game.Debris:AddItem(thrownTool, 5)
else
warn("Handle not found in the tool model.")
end
end)
may i see a gif or video of what happens when the ball is thrown? it may help diagnose the issue, it could be because of networkownership changes causing weird behavior, when you clone the ball you may not CFrame it to the right position despite being cloned, so it may take an indirect or awkward path to the mouse position, etc.
could you try printing both the client and the servers targetPosition? it looks like your mouse in first person may be clipping a part of your accessory (pure speculation) and it could be that the targetPosition in general may be inaccurate
Do you see the line with local throwDirection = (targetPosition - handle.Position).unit its where im getting the direction.
But my client is
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local damageEvent = ReplicatedStorage:WaitForChild("DamageEvent")
local throwToolEvent = ReplicatedStorage:WaitForChild("ThrowToolEvent")
local throwCooldown = 1.5
local canThrow = true
tool.Activated:Connect(function()
if not canThrow then return end
canThrow = false
local targetPosition = mouse.Hit.Position
if targetPosition then
print("targetPos:", targetPosition)
end
throwToolEvent:FireServer(targetPosition)
wait(throwCooldown)
canThrow = true
end)
damageEvent.OnClientEvent:Connect(function(humanoid)
end)
you should place a part where the server and client said it saw the mouse position and reference it with where you clicked in reality, it’s a good way to debug if the code is doing as you wanted. if it is the case, it could be that the ball is having its velocity altered which could be another issue entirely.