I was working on a simple touch football script that just applies velocity based on the angle the player hits the ball at. I kind of feel like using .Touched can be a little delayed and inaccurate sometimes, so I was just wondering what’s the best way to do this as well as a little explanation on how to do it.
Heres my current ball script:
local Players = game:GetService("Players")
local REACT_DECLINE_COOLDOWN = 0.7
local IGNORE_LIST = {"Right Arm", "Left Arm"}
local ball = script.Parent
local properties = ball:WaitForChild("Properties")
local reactDecline = properties:WaitForChild("ReactDecline")
local owner = properties:WaitForChild("Owner")
local function onTouched(otherPart: BasePart)
if reactDecline.Value then
return
end
if table.find(IGNORE_LIST, otherPart.Name) then
return
end
local character = otherPart.Parent
if character:FindFirstChild("Humanoid") then
local player = Players:GetPlayerFromCharacter(character)
ball:SetNetworkOwner(player)
owner.Value = ball:GetNetworkOwner()
local direction = CFrame.lookAt(character.HumanoidRootPart.Position, ball.Position).LookVector
if direction.Magnitude < 0.001 then
return
end
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyVelocity.Velocity = direction.Unit * 28
bodyVelocity.Parent = ball
task.delay(0.3, function()
bodyVelocity:Destroy()
end)
reactDecline.Value = true
task.delay(REACT_DECLINE_COOLDOWN, function()
reactDecline.Value = false
end)
end
end
ball.Touched:Connect(onTouched)
In my game and the games it was based off, there was a way to work with the Touched event so that it would be almost completely accurate. In those games, people can shoot flying parts that turn to the mouse’s position in order to try to hit other characters; which means an accurate hitbox is needed.
In my game, while using the Touched event to detect hits, I also have a separate Stepped connection that would constantly fix the part’s position, which makes Touched work much better:
Also if there are worries about the performance, in my game we could have 10 players constantly using the skills that spawn those parts without any notable performance issue. Since there haven’t been any servers with more than 10 players at a time, I have no idea how it would be with more players than that.
You mean like why that makes Touched work well I imagine?
To be honest, I’m not sure if my answer is correct, but based on what I’ve experienced, it’s like the Touched event considers the view of the clients; so if it has Touched in the view of a single client, even though not in the server, it would fire.
Considering this, when we change the CFrame of a BasePart, the server will obligate the client to reload the position of that BasePart to its new position, where a small teleport happens.
So what I imagine is that if we’re constantly making the server update the CFrame of the part through code, the clients would be constantly realigning the CFrame to what it is on the server (remember BodyVelocities play on the client). With that, the difference between where the BasePart is on the client compared to the server would be much smaller than if we didn’t update it and only used a BodyVelocity to move it.
However, this would mean that if a player lags, the CFrame of the BasePart would not be updated correctly and there would be the possibility of the Touched event firing incorrectly. Well, but that simply doesn’t happen; regardless of lag or anything, it simply works very well in my game with 100% accuracy.
Even though it works very well in my game, I’m afraid it could not work just as well with very high velocities (+140) or with very small/big BaseParts. I haven’t tried it yet.