Tool.Activated Confusion!

  1. What do you want to achieve?
    I want to enable my Tool.Activated function to pass 2 values from client to server on one activation, but to pass a different 2 values on the second activation, and require both activations for anything to occur.
  2. What is the issue?
    Currently, when the player clicks with the tool equipped, the script waits for a second activation of the player clicking to fire a remote event to the server with the information of the first location of the mouse when the player clicked and the torso position of the player when they clicked, and when the player clicks a second time, the value of their new mouse position and torso position. Currently, when the player clicks once, and then twice, it works as expected, with the correct values being passed and the server responding correctly. However, after these first two activations, the client only fires the second activation portion of the local script! I am positive that there is some error or flaw in how I am waiting for the detection of two clicks in succession from the client-side.

I should also note that the server script has a sort of cool down, causing it to not produce any models (which is the function of it) until it has run the entire script, despite how much the client clicks.


Above pictures what happens the first time the player uses the tool by clicking twice in different locations (what is supposed to happen)


Above pictures a single click from the player, with the second mouse detection only being used, and the first mouse detection value remaining unchanged.

So my final question would be how would I keep the local script requiring two clicks from the player to fire to the server, and use the values in the correct order of activation (ex. the first model spawns where the first click is, and the second model spawns where the second click is)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("gargantaEvent")
local Tool = script.Parent
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local character = player.Character
local mouse = player:GetMouse()
local RunService = game:GetService("RunService")

Tool.Activated:Connect(function()
	print("Click Detected")
	local mousePosition = mouse.Hit.p
	local torsoPosition = character.Torso.Position
	repeat wait() until Tool.Activated:Connect(function()
		print ("Click 2 Detected")
		local mousePosition2 = mouse.Hit.p
		local torsoPosition2 = character.Torso.Position
		remoteEvent:FireServer(mousePosition, torsoPosition, mousePosition2, torsoPosition2)
	end)
end)

Above is the local script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("gargantaEvent")
local TweenService = game:GetService("TweenService")
local clicked = false

local function onRemoteEvent(player, mousePosition, torsoPosition, mousePosition2, torsoPosition2)
	if not clicked then
		clicked = true
		
		print "remote event detected on server"
		local gargantaModelClone = game.ServerStorage.gargantaModel:Clone()
		gargantaModelClone.Parent = game.Workspace
		game.Workspace.gargantaModel:SetPrimaryPartCFrame(CFrame.new(mousePosition, torsoPosition) * CFrame.Angles(math.rad(-90), math.rad(0), math.rad(180)))

		
		local gargantaModelClone2 = game.ServerStorage.gargantaModel2:Clone()
		gargantaModelClone2.Parent = game.Workspace
		game.Workspace.gargantaModel2:SetPrimaryPartCFrame(CFrame.new(mousePosition2, torsoPosition2) * CFrame.Angles(math.rad(-90), math.rad(0), math.rad(180)))

clicked = false
remoteEvent.OnServerEvent:Connect(onRemoteEvent)

Above is the relevant information from the server script

1 Like

Instead of doing repeat wait() until Tool.Activated:Connect(function()
do Tool.Activated:wait()

I tried your solution but sadly it didn’t result in any sort of change. :frowning:

It might be that your not reseting mousePosition and mousePosition2 so one of them (I’m assuming its mousePosition2 ) is using somewhere you previous clicked last time. So why not right after

	print("Click Detected")

You set both mousePosition and mousePosition2 to nil. Thats my guess at what could be the issue.

Also every time you click again your creating a new .Activated connection. So eventually when you click once, it will print(“Click 2 detected”) like 10 times.

You can alternatively do this instead of your current method:

local Click1Position; -- Create a variable to store click 1
local Click2Position; -- Create a variable to store click 2

Tool.Activated:Connect(function()
	if not Click1Position then -- Check if Click1Position isn't set (nil)
		Click1Position = mouse.Hit.p; 
	elseif Click1Position and not Click2Position then -- Check if Click1Position is set and Click2Position isn't (nil)
		Click2Position = mouse.Hit.p;
	end;

	if Click1Position and Click2Position then -- Check if both clicks have been set
		print("fire server with data"); -- Send data
		print(Click1Position)
		print(Click2Position)
		print("--------------")	
		Click1Position = nil; -- Reset Click1Position
		Click2Position = nil; -- Reset Click2Position
	end;
end);
1 Like