Hi! I’m making an elemental game (like elemental chaos divine duality elemental battlegrounds etc) and I’m having trouble making a fireball fire at the mouse’s position. If anyone could help that would be awesome. Thanks! Here’s the script I have right now:
(In ServerScriptService)
game.ReplicatedStorage.Events.FireBall.OnServerEvent:Connect(function(player,cframe)
local character = player.Character
local mouse = player:GetMouse().Hit.Position
local Fireball = fire1:Clone()
Fireball.CFrame = character.HumanoidRootPart.CFrame
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.MaxForce = Vector3.new(5000,5000,5000)
bodyVelocity.Velocity = (mouse - character.HumanoidRootPart.Position).Unit*100
bodyVelocity.Parent = Fireball
for some reason, I keep coming up with this error:
ServerScriptService.FireballHandler:5: attempt to index nil with ‘Hit’
The error is because you can only do Player:GetMouse() in the client (LocalScript), since the player’s mouse won’t replicate over to the server.
To actually move the fireball using the client you could either:
Send the mouse position information to the server and let it handle it (instead of in the server attempting to player:GetMouse().Hit.Position you can create a RemoteFunction that asks the player to perform that operation and return the result).
Move all of this to the client and grant the network ownership of the fireball clone to the player.
The second option is not really that recommended since it could be exploited more easily, however, it could be convenient in some specific instances, for example, when it’s imperative for the experience to the player not experiencing any replication lag.
Body Velocity is depracated and should not be used for further work.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FireBallEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("FireBall")
FireBallEvent.OnServerEvent:Connect(function(player, mousePosition)
local character = player.Character
if not character then return end
local Fireball = fire1:Clone()
Fireball.CFrame = character.HumanoidRootPart.CFrame
Fireball.Parent = game.Workspace -- Ensure the fireball is parented to the workspace
local linearVelocity = Instance.new("LinearVelocity")
linearVelocity.MaxForce = Vector3.new(5000, 5000, 5000)
linearVelocity.VectorVelocity = (mousePosition - character.HumanoidRootPart.Position).Unit * 100
linearVelocity.Parent = Fireball
local attachment = Instance.new("Attachment")
attachment.Parent = Fireball
linearVelocity.Attachment0 = attachment
end)