Where should I put this else or do I need a elseif statement?

Have I placed this else in the wrong spot and do I need a elseif statement?

Tool.Activated:Connect(function()
	if Mouse.Target then
		local Model = Mouse.Target:FindFirstAncestorOfClass("Model")
		
		if Model then
			if Model:FindFirstChildWhichIsA("Humanoid") then
				ToolAttackEvent:FireServer(Model)
			end
		end
	else
		ToolActivateEvent:FireServer()
	end
end)

The ToolActivateEvent:FireServer() sends a request to server to do some checks, then rewards the player cash

Could achieve the same thing through a Click detector and checking which tool the player currently has equipped instead of giving the client power.
Your else statement will only ever run if your mouse is hovering over nil (skybox), as you haven’t really explained what you want to achieve I can’t say if that’s right or wrong.

The remote event gets fired from the client to the server, the server the does some checks and then the cash is added to the player through the server. This event is fired when the tool is activated (clicked when equipped).

I am just not sure if I should put the else or elseif statement if I need it:

Could I possibly do this:

Tool.Activated:Connect(function()
	ToolActivateEvent:FireServer()
	
	if Mouse.Target then
		local Model = Mouse.Target:FindFirstAncestorOfClass("Model")
		
		if Model then
			if Model:FindFirstChildWhichIsA("Humanoid") then
				ToolAttackEvent:FireServer(Model)
			end
		end
	end
end)

Again you’re explaining the purpose of the event. You’re not explaining what exactly you’re trying to achieve. For example what scenario do you want to have it work in? Based with your current code snippet every time you click whilst holding the tool it will fire the remote, is this what you want to be happening? I’m confused on exactly what you want, you haven’t really explained what you want to happen which makes solving the OP title impossible.

Ok. Hopefully this helps.

I am checking if the mouse has detected a target which has a Humanoid if not it will just give the player cash and if it does detect a Humanoid then it will fire a different remote event named ToolAttackEvent to the server to do a certain action. My problem is have I written this code out correctly?

Tool.Activated:Connect(function()
	ToolActivateEvent:FireServer() -- Fires when the tool is clicked when equipped
	
	if Mouse.Target then -- The block inside this code runs if the mouse detects something with a humanoid
		local Model = Mouse.Target:FindFirstAncestorOfClass("Model")
		
		if Model then
			if Model:FindFirstChildWhichIsA("Humanoid") then
				ToolAttackEvent:FireServer(Model) -- Fires event to do action on the server
			end
		end
	end
end)
Tool.Activated:Connect(function()
	if Mouse.Target.Parent:FindFirstChild("Humanoid") then 
		ToolAttackEvent:FireServer(Mouse.Target.Parent) 
	else
	    ToolActivateEvent:FireServer() 
	end
end)
1 Like