Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I wanted my script to do something when clicking with a tool but is when its touched?

I have tried a few different scripts and slightly edited them, however whenever I try it it always ends up adding the value when the tool touches the object. I am trying to make it so when you click the part with a tool it adds one onto the value, however it only adds on when you touch it. Help would be greatly appreciated. :)

The (Local) Script:

local tool = script.Parent
local handle = tool.Handle

function onActivate()
    handle.Touched:Connect(function(otherPart)
        if otherPart.Name == "Wood" then
            game.Workspace.woodCount.Value = game.Workspace.woodCount.Value + 1
            game.StarterGui.wood.TextLabel.Text = game.Workspace.woodCount.Value
else
	if otherPart.Name ~= 'Wood' then
		print("no")
	end
        end
    end)
end



tool.Activated:Connect(onActivate)

1 answer

✓ Accepted answer

First let me clean up the code you have quick:

local tool = script.Parent
local handle = tool.Handle
local player = script.Parent.Parent.Parent -- This (should) give you the player that's holding the tool

function onActivate()
    handle.Touched:Connect(function(otherPart)
        if otherPart.Name == "Wood" then
            workspace.woodCount.Value = workspace.woodCount.Value + 1
            player.PlayerGui.wood.TextLabel.Text = workspace.woodCount.Value -- Change our player's GUI, not the StarterGui!
		else
			print("Not wood");
        end
    end)
end

tool.Activated:Connect(onActivate)

And now just an explanation for the current behavior:

What you have now will: a) when the user clicks, create a new function and b) set up the newly created function to run every time the Handle is touched by something.

So if you don't click at all, the value will never be incremented. If you click once, nothing will happen until you touch a brick, in which case value will increase by 1. If you click twice, nothing will happen until you touch a brick, in which case value will increase by 2 because two functions will run.

The fix

You have a few options. Maybe one of the easiest to understand would be to just disconnect the Touched event after a period of time, like this:

local tool = script.Parent
local handle = tool.Handle
local player = script.Parent.Parent.Parent

local waitTime = 0.3 -- Amount of time to keep the .Touched event open

local connection;

function onActivate()
	-- Disconnect any existing connection
	if (connection) then
		connection:disconnect()
		connection = nil
	end
	
    connection = handle.Touched:Connect(function(otherPart)
        if otherPart.Name == "Wood" then
            workspace.woodCount.Value = workspace.woodCount.Value + 1
            player.PlayerGui.wood.TextLabel.Text = workspace.woodCount.Value
			connection:disconnect() -- don't need to call this ever again
			connection = nil
		else
			print("Not wood");
        end
    end)
	
	-- After a certain amount of time, stop listening for Touched events.
	wait(waitTime)
	
	if (connection) then
		connection:disconnect()
		connection = nil
	end
end

tool.Activated:Connect(onActivate)

Another way (which I personally think is better) would be to always listen for the .Touched event, and have the .Activated event control whether or not it does anything:

local tool = script.Parent
local handle = tool.Handle
local player = script.Parent.Parent.Parent

local waitTime = 0.3 -- Amount of time to keep the .Touched event open

local canIncrease = false -- Will set this to true for a moment when the mouse is clicked

handle.Touched:connect(function(otherPart)
	if (not canIncrease) then return end -- quit the function if we can't increase
	
	-- Otherwise...
	if (otherPart.Name == "Wood") then
		workspace.woodCount.Value = workspace.woodCount.Value + 1
		player.PlayerGui.wood.TextLabel.Text = workspace.woodCount.Value
		canIncrease = false -- don't run this again
	end
end)
		

tool.Activated:connect(function()
	canIncrease = true
	wait(waitTime)
	canIncrease = false -- could also move this to the "Deactivated" event if you desire
end)

Note that this will probably have some strange behavior if you click multiple times very rapidly. I'll leave it as an exercise for you to figure out how to fix that, if you're feeling like it needs to be fixed.

1 comment

0

Wow, thanks for the detailed response. I definitely learnt from this! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.