Fireball script wont work when I right click

I made a script that is supposed to shoot a fireball when you right-click while holding the tool, but it doesnt work

How can I fix this? Am I missing something?

No Errors in the output either

local Tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local mouseButton = "" 
UserInputService.InputBegan:Connect(function(input, gpe)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then 
		mouseButton = "RIGHT"
		Tool:Activate()
	end
end)

Tool.Activated:Connect(function()
	if mouseButton == "RIGHT" then
		local remote = game.ReplicatedStorage.FireballEvent
		remote.OnServerEvent:Connect(function(player, MousePosition)
			local fireball = game.ReplicatedStorage.Fireball:Clone()
			fireball.Parent = workspace
			fireball.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-2)
			local NewForce = Instance.new("BodyForce")
			NewForce.Force = Vector3.new(0, workspace.Gravity * fireball:GetMass(), 0)
			NewForce.Parent = fireball
			fireball.Velocity = CFrame.new(player.Character. HumanoidRootPart.Position, MousePosition).LookVector * 50
			fireball. Touched:Connect(function(hit)
				if hit. Parent:FindFirstChild ("Humanoid") and not hit:IsDescendantOf(player.Character) then hit.Parent:FindFirstChild ("Humanoid"): TakeDamage(50)
					fireball:Destroy()
				end
			end)
		end)
	end
end)

Will someone please let me know! Thank you!

1 Like

Instead of using the UIS to activate the tool, just check if its equipped and run the function in UIS

tool.Activated only runs when your input is the left mouse button. So what you should do is make a boolean that changes when the tool is equipped/unequipped and once the user has clicked on the right mouse button, if the boolean is true then make the fireball on the server.

1 Like

I’m guessing the script is not a local script, UserInputService can only be used by local scripts, therefore a server sided script cannot detect a player’s inputs. That might be why it’s not giving anything on the output.

1 Like

to fix this, use userinputservice to check if the player right clicks their mouse on the client. then use a remote event to send the mouse data to the server, and then do the fireball stuff on the server

I tried to edit your code and tried to solve your problem, heres my solution to it:

Make local script inside the tool, then put this piece of code in:

local Tool = script.Parent
local UserInputService = game:GetService("UserInputService")

local mouse = game:GetService("Players").LocalPlayer:GetMouse() -- To send the player's mouse's position, we get player's mouse.
local remote = game.ReplicatedStorage.FireballEvent
local equipped = false -- A boolean value that changes based on whether if the player has equipped the tool or not.

UserInputService.InputBegan:Connect(function(input, gpe)
	if input.UserInputType == Enum.UserInputType.MouseButton2 and equipped then -- Checks if the player presses right mouse button and the boolean value (equipped) is true.
		remote:FireServer(mouse.Hit.Position) -- Fires a remote event and sends the mouse position.
	end
end)

Tool.Equipped:Connect(function() -- When the tool is equipped, sets the boolean value (equipped) to true.
	equipped = true
end)

Tool.Unequipped:Connect(function() -- When the tool is unequipped, sets the boolean value (equipped) to false.
	equipped = false
end)

Like how @nontoxicguy1 said, we use a boolean variable to detect if the tool is equipped or not, and when the player presses the right mouse button, we fire a remote event so the server makes a fireball.

Then, we make a server script, and put it inside ServerScriptService, and then we put the code you had already written inside it:

local remote = game.ReplicatedStorage.FireballEvent

remote.OnServerEvent:Connect(function(player, MousePosition)
	local fireball = game.ReplicatedStorage.Fireball:Clone()
	fireball.Parent = workspace
	fireball.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-2)
	local NewForce = Instance.new("BodyForce")
	NewForce.Force = Vector3.new(0, workspace.Gravity * fireball:GetMass(), 0)
	NewForce.Parent = fireball
	fireball.Velocity = CFrame.new(player.Character. HumanoidRootPart.Position, MousePosition).LookVector * 50
	fireball. Touched:Connect(function(hit)
		if hit. Parent:FindFirstChild ("Humanoid") and not hit:IsDescendantOf(player.Character) then hit.Parent:FindFirstChild ("Humanoid"): TakeDamage(50)
			fireball:Destroy()
		end
	end)
end)

After that, your fireball tool should work now! However, I suggest using VectorForce instead of BodyForce since it’s deprecated.

Also, apologies if I had done something wrong while writing this, this is my first time using the DevForum.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.