so i was watching a YouTube video on how to make a Fireball shoot out but it shot out and forward from your torso, so i tried to edit it and make it shoot at the mouse position as this
local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local RemoteEvent = game.Workspace.Fired local UIS = game:GetService("UserInputService") local Debounce = false Mouse.Button1Down:Connect(function() if Debounce == true then return end RemoteEvent:FireServer(Debounce) end)
That's my FireBall script in my StarterPack
and this is my FireBall Event Script
game.Workspace.Fired.OnServerEvent:Connect(function(Player, Debounce) local Character = Player.Character or Player.CharacterAdded:wait() Debounce = true local Mouse = Player:GetMouse() local mH = Mouse.Hit local Fireball = Instance.new('Part') Fireball.Shape = Enum.PartType.Ball Fireball.BrickColor = BrickColor.new('Deep orange') Fireball.Transparency = '0.5' Fireball.Size = Vector3.new(2,2,2) Fireball.CanCollide = false Fireball.Material = Enum.Material.Neon Fireball.Parent = Character Fireball.CFrame = Character.Torso.CFrame*CFrame.new(0,1,-6) local BV = Instance.new("BodyVelocity") BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge) BV.Velocity = Character.mH.p.CFrame.lookVector*100 BV.Parent = Fireball wait(2) Debounce = false Fireball:Destroy() end)
could you please help me to find why this error shows up
11:29:06.292 - Workspace.Fired Event:5: attempt to index local 'Mouse' (a nil value) 11:29:06.293 - Stack Begin 11:29:06.295 - Script 'Workspace.Fired Event', Line 5 11:29:06.296 - Stack End
I Also have a Fired RemoteEvent in my workspace
ps: im learning to code lua and a game with my friend from watching tutorials and getting to know what specific stuff does, and with things like this. iv'e never asked a question until now but is that a good way to learn to code or should i try a different approach
Player:GetMouse
returns nil
on the server side, because the mouse instance is created on the client. It does not make sense for the server to handle mouse logic.
If the server needs mouse.Hit
, why not send it to the server directly?
lua
RemoteEvent:FireServer(mouse.Hit);
Mouse
events have been superseded by UserInputService
and ContextActionService
events/functions.
You had declared UIS
but never used it. Why not use it?
In addition, remote events/functions should always be in ReplicatedStorage
, unless they specifically need to be elsewhere, like a Tool
. I don't see a need for it in Workspace
in your situation.
```lua local UserInputService = game:GetService("UserInputService"); local ReplicatedStorage = game:GetService("ReplicatedStorage"); local Players = game:GetService("Players");
local client = Players.LocalPlayer; local cursor = client:GetMouse(); local canShoot = true;
UserInputService.InputBegan:Connect(function(input, gameProcessed) --[[ /* input is the inputObject, which contains properties like UserInputType, KeyCode, ect. gameProcessed is a boolean that is true if the event fired because of interaction with the game engine, like typing in a textbox, e.g. chatting, or if you click a gui button */ --]]
if (gameProcessed or not rawequal(input.UserInputType.Name, "MouseButton1") or not canShoot) then return; --// end function since interacted with engine, or they did *not* press left mouse button, or they cannot shoot end canShoot = false; ReplicatedStorage.Fired:FireServer(cursor.Hit.Position); --// sending only the position; you don't need the whole cframe or do you ??? wait(2); canShoot = true;
end); ```
And on the server side...
```lua local ReplicatedStorage = game:GetService("ReplicatedStorage");
--// constants local BALL_SIZE = Vector3.new(2, 2, 2); local MAX_FORCE = Vector3.new(math.huge, math.huge, math.huge); local BALL_COLOUR = BrickColor.new("Deep orange");
ReplicatedStorage.Fired.OnServerEvent:Connect(function(client, position) local character = Player.Character; local fireball = Instance.new("Part"); fireball.Shape = Enum.PartType.Ball ; fireball.BrickColor = BALL_COLOUR; fireball.Transparency = 0.5; --// doesn't need to be string fireball.Size = BALL_SIZE; fireball.CanCollide = false; fireball.Material = Enum.Material.Neon; fireball.CFrame = character.Torso.CFrame*CFrame.new(0, 1, -6); fireball.Parent = character;
local bodyVelocity = Instance.new("BodyVelocity"); bodyVelocity.MaxForce = MAX_FORCE; bodyVelocity.Velocity = character.HumanoidRootPart.CFrame.LookVector*100; bodyVelocity.Parent = fireball; wait(2); fireball:Destroy();
end); ```