hello, I am new too scripting but I know a lot already. Recently I came up with the idea to make a ball throwing game so today I got into studio, added some UI and made the ball. I’ve added some scripts into it but I faced a problem:
1: I don’t know how to make the ball throw and then reappear and give damage
2: I don’t know how to make a cool down. how would I do that?
You can use a small part that the Weld can hang up the Part which is the ball.
And the ball is unanchored.
You will need a RemoteEvent to tell the server what direction the player wants to throw the ball in. You can calculate that direction by using UserInputService:GetMouseLoction() and turn that mouse position into a Vector, using game.Workspace.CurrentCamera.ViewportPointToRay. After the server has been given the direction ray from the player, then you can create a ball on the server flying in that direction.
The reappearing, you can do by just toggling the ball your character is holding to be fully transparent/not. That way you don’t need to remake the ball you’re holding each time. While you’re toggling the transparency, you can set a variable to true for “reloading” then set it to false after a set amount of time. You then check whenever a player tries to throw a ball, and make sure that your Reloading variable is false, before you let them.
I am confused on what you are saying, could you explain your solution a bit further?
I see what you mean now! And also it is ok, I just didn’t want to feel like I copied someone’s scripts.
Is this what you mean weeve? As I said I am not really a good scripter
For your second part You could use Debounce.
Its a way of setting cooldowns.
For example
local debounce = false
function real(hit)
if not debounce then
debounce = true
-- random functions and scripts here
wait(10)
debounce = false
end
end
Instead of using camera.ViewportSize/2 as your viewportPoint, use the GML. And instead of setting ball.position to GML, set ball.Velocity to unitRay.Direction*Speed where Speed is a number in studs per second.
You will also want this to be done in a server script, rather than a local script, and that’s what you’d need the RemoteEvent for. Though if are using a tool, you can avoid the need for that, and just use Tool.Activated through a server script, which I believe replicates for you.
I have a feeling this is a bit more complicated of a task than you’re ready for, in programming, though. I don’t mind continuing to help until you do eventually figure it out, but it might be a while.
I did that but it did not work
thanks man I will use that in my script
I am clueless at this point
Thanks for the help! even though it did not work, I keep trying
Im not that good of a scripter but you can try using Mouse.Hit
https://developer.roblox.com/en-us/api-reference/property/Mouse/Hit
Mainly used for projectiles
k thanks for the advice n4dar.
here is basically what you will need to do.
before you do anything, add a simple remote event to your tool. Remote Events basically allow you to communicate from the server to the client.
In your tool, add a local script that has this code.
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse() -- this is also a depricated feature. using UserInputService would be better
local tool = script.Parent
local remote = tool:WaitForChild("RemoteEvent")
remote.OnClientEvent:Connect(function() --- this remote event could need some more protection from hackers. A simple method of this would be to check if the item is equipped.
remote:FireServer(mouse.Hit.Position) --- this will give you the position of the mouse.
end)
Now, add a server script into your item that has something like this
local Player = nil
local Character = nil
local Humanoid = nil
--- these are all forward delarations that will be used for all parts of the script
local Debris = game:GetService("Debris")
local Tool = script.Parent
local Fireball = Tool.FireBall -- change this to whatever part you want to be thrown basically
local Debounce = false
local RemoteEvent = Tool:FindFirstChild("RemoteEvent")
Tool.Equipped:Connect(function()
Character = Tool.Parent
Player = players:GetPlayerFromCharacter(Char)
Humanoid = Char:FindFirstChild("Humanoid")
end)
Tool.Activated:Connect(function()
if Debounce then return end --- this will make it so if the Debounce is true, the script wont run
Debounce = true
RemoteEvent:FireClient(Player)
wait(5)
Debounce = false
end)
RemoteEvent.OnServerEvent:Connect(function(player,mousePosition)
--- do your stuff here basically but i will provide an example
local Direction = mousePosition - Tool.Handle.Position
local Projectile = Fireball:Clone()
Projectile.Parent = workspace
local Velocity = instance.new("BodyVelocity") -- this is a depricated feature, but i will use it for this example
Velocity.MaxForce = Vector3.new(1e6,1e6,1e6) -- don't use math.huge for MaxForce.
Velocity.Velocity = CFrame.new(Fireball.Position,mousePosition).LookVector * 30 -- change the 30 to your speed
Velocity.Parent = Fireball
Fireball.Parent = workspace
Debris:AddItem(Fireball,5) -- how long you want until the Fireball Basically Disappears
-- this is if you want the fireball to do damage
Fireball.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = humanoid.Health - 25 -- the damage you want
Fireball:Destroy()
end
end)
end)
I havent tested this out, so there might be some typos!
There is no camera.GML, since GML is just your variable name. You don’t need line 9 anymore, the way you wrote this.
k alright I will do this now, thanks.