First off: Welcome to the community!
I'm glad you're trying to script; scripting is not by any means easy and takes a lot of time. Most of the decent scripters nowadays have gathered knowledge from experience, experiments, and research that spans over months, maybe even years.
Script Help
If you ever need help, this is the perfect place for you. Just keep in mind, if you want your questions to be answered quickly and as perfectly as possible, refer to our community guidelines. (This was a perfect question, I'm just saying this for future reference!) If you ever want to learn more about scripting, I made a little post over on this page and ScriptingHelpers also made a page on how to help new scripters. Both here and here.
Now onto your script
local barrel = game.Workspace.Cannon.Barrel
local fireballcopy = game.ReplicatedStorage.Fireball
local cannon = script.Parent
local start = game.Workspace.Cannon.start
start.ClickDetector.MouseClick:connect(function(hit)
hit.Parent = game.Workspace
if start.ClickDetector.MouseClick:connect() then
fireballcopy:Clone()
fireballcopy.Position = barrel.Position
end
end)
A few important notes; I would recommend researching WaitForChild, Local Scripts, Scripts, Module Scripts, and if statements. This information may not pertain to this script exactly, but it's common knowledge that will help you in the future.
WaitForChild
WaitForChild is an action that will have your script wait until a certain object is loaded. Since this is an action, you would use a colon instead of a period, much like how you used connect. This is an essentiality for scripts as you might try to refer to an object that doesn't exist, and then your script gets mad and quits. You would then follow with parenthesis and the name of the object in parenthesis.
An example of its use is
local part = game.Workspace:WaitForChild("Part")
You can even use this in chains!
local partsPart = game.Workspace:WaitForChild("Part"):WaitForChild("Part")
This variable would refer to a part that's inside of a part.
Local Scripts, Scripts, Module Scripts
Now, this isn't really topical, but I still find that I should explain for future reference.
As you may know, there is a client and a server. The server is the game that the player (client) has joined. The client is really just a fancy term for the player's point-of-view and the player's side.
An analogy for this would be a neighborhood (server) with little houses (client). Each neighborhood has their owns houses in it.
A script would be used for the neighborhood and a local script would be used for the house.
Module scripts don't really have a position. Module Scripts hold functions that you can use over-and-over again in both local and server scripts. Kind of like electricity I suppose. You can use electricity to power the street lights (neighborhood) and use electricity to power your house lights (house).
Connect functions and If statements
I noticed that you used a connect function and then followed it with an if statement verifying that the connect happened. This really isn't necessary nor is it functional.
By using start.ClickDetector.MouseClick:connect you are automatically telling the script: If this thing gets clicked, then run this code so there's no need for an if statement verifying it's authenticity.
Now onto your scripts errors
Lines 1, 2, and 4 do not use WaitForChilds, therefore they are automatically assuming the objects are loaded and there, even if they're not.
ClickDetector's first argument (which you referred to as hit) is the player that clicked it. Keep in mind it's not the character, it's the literal player.
You used hit.Parent = game.Workspace which is the main cause of your error. I would just take this line out.
On line 8 you made an unnecessary, non-functional if-statement. Just take this out.
On line 9 you made a clone, then on line 10 changed the original fireball's parent. To fix this, I would make the clone a variable and then change the clone's position, rather than the fireball's position. You also did not give the clone a parent, so it had no place to go, making the clone lonely and depressed since you gave it no home. Now how do you feel?
Here is the completed script
local barrel = game.Workspace:WaitForchild("Cannon"):WaitForChild("Barrel")
local fireball = game:GetService("ReplicatedStorage"):WaitForChild("Fireball")
local cannon = script.Parent
local start = game.Workspace:WaitForChild("Cannon"):WaitForChild("start")
start.ClickDetector.MouseClick:connect(function(player)
local fireballcopy = fireball:Clone()
fireball.Parent = barrel
fireballcopy.Position = barrel.Position
end)
I hoped this helped you! If it did, please click Accept Answer.
3 comments
i dont think you need to put "If start.ClickDetector.MouseClick:connect() then" on line 8, i might be wrong, also you can not change the parent of the player, meaning you cant change it to workspace which is what your doing, i think
MouseClick returns the Player, and you can't parent the Player out of "Players".
Now i'm having trouble with the if statement Idk what to do on that part any help?