So I’m making a game where there is a ball and players can hit it using mousebutton1 but they dont exactly click on it, its just if they are within like a 5 stud radius if they press mousebutton1 it launches the ball away at another random target player. And the point is after 45 seconds the round is over, any idea how I can start this? I’m a pretty novice scripter so I dont need exact scripts just parts of them and also some explanations on how I can do it.
You can make it so if a player clicks their mouse, a remote event is fired. Then on the server side, if the player is close enough to the ball, you then find a random player to launch it at, and then it will do some math calculations to figure out the direction of the velocity / force that should be applied to the ball, and then have some value to multiply this direction by that determines the speed of the ball. Then have a touched event inside of a script that is inside of the ball or something, so when the ball touches a player, that player looses health, or is out of the game or something.
Okay, that makes sense. I think I got this.
Ok for the hitting a ball system, try something like this. Sorry if the code is confusing!! I’m not really that great at explaining things
local MaxDistance = 5
local function findRandomPlayer()
local listOfPlayers = game.Players:GetPlayers()
local listOfCharacters = {}
for _, character in pairs(listOfPlayers) do
table.insert(listOfCharacters, character)
end
local randomNumber = math.random(1, #listOfCharacters)
--When you put # in front of a table, it returns how values are in the table
local randomCharacter
for int, character in pairs(listOfCharacters) do
if int == randomNumber then
randomCharacter = character
end
end
return randomCharacter
end
Player.MouseButton1Down:Connect(function()
local Ball = workspace.Ball
local character = Player.Character
if not ball or not character then
return --This script doesn't need to run if there isn't a ball or character
end
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local ballToCharacter = Ball.Position - humanoidRootPart.Position --Returns the Vector3 between the ball and the character
if ballToCharacter.Magnitude <= MaxDistance then
--Magnitude is a Vector3's length
--Now we launch the ball to a random character
local randomCharacter = findRandomPlayer()
end
end)
Edit: I just realized this would all be on the client, so use a remote event like what TritiumAndDeuterium said
Would this code still work I get its client side but cant you still use this?
It would work client side, but I it would be a vulnerability to exploiters. Exploiters use exploits on the client. Since they can manipulate the client and the script is on the client, exploiters could take this to your advantage. Also you can use my code, but I left a few variables undefined (like the player).
I’m not really sure how to go with your code, I’d need to ask around on some Discords for help but hopefully I can figure it out.
If you need help with any of the math for calculating those directions for the forces / velocity of the ball, feel free to ask again (Reply to my reply or DM me), and I might be able to help with a bit more detail.