Hi, I am having trouble with my last if statement. The if statement is not printing clicks and I don’t know why. If you can explain and fix this it would be great.
local clicks = 0
local Player = Game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local pos1 = nil
local pos2 = nil
Mouse.Button1Up:Connect(function()
if clicks == 0 then
clicks = 1
pos1 = Mouse.Hit.p
print(pos1)
end
end)
Mouse.Button1Up:Connect(function()
if clicks == 1 then
clicks = 2
pos2 = Mouse.Hit.p
print(pos2)
end
end)
if clicks == 2 then
print(clicks)
end
The if statement is ran on runtime, so it is working, but you can’t tell cause clicks
isn’t 2
at first, you’d have to put the if statement in one of the events so it can detect it
1 Like
I have done this and it worked thank you
1 Like
I would suggest using UserInputService rather than the PlayerMouse, because the PlayerMouse is deprecated.
I would also suggest rather than indexing services, to use ServiceProvider:GetService
.
local PlayersService: Players = game:GetService("Players")
local GuiService: GuiService = game:GetService("GuiService")
local UserInputService: UserInputService = game:GetService("UserInputService")
local LocalPlayer: Player = PlayersService.LocalPlayer
local PlayerMouse: PlayerMouse = LocalPlayer:GetMouse()
local pos1, pos2
local ClickCounter: number = 0
UserInputService.InputEnded:Connect(function(InputObject: InputObject, GameProcessedEvent: boolean)
if not GameProcessedEvent and InputObject.UserInputState ~= Enum.UserInputState.Change and not UserInputService:GetFocusedTextBox() and not GuiService:GetEmotesMenuOpen() then
if InputObject.UserInputType == Enum.UserInputType.MouseButton1 and InputObject.UserInputState == Enum.UserInputState.End then
ClickCounter = ClickCounter + 1
if ClickCounter == 1 then
pos1 = PlayerMouse.Hit.Position
elseif ClickCounter == 2 then
pos2 = PlayersService.Hit.Position
end
print("The click counter has updated to " .. ClickCounter)
end
end
end)
The statement may not work as expected because you’re not checking the updated click counter, the statement would belong in the event.