Looking for help on Events

Hello Everyone my name is Matthew, You guys can call me Matt.

I’m having a little issue with some code, Overall I’m pretty good at it just have a hard time putting it together.

My issue is I’m trying get the correct Event for a tool… When they equip the tool and click something is suppose to happen. I tried looking the the explorer but Just had no use, tried using some of the Grip events, nothing.

Maybe I just did it wrong.

All I need the the Proper Event.

here is the Code…

local base = game.Workspace.Baseplate
-- Calling a Variable for the Sword
local sword = game.StarterPack.ClassicSword
-- Calling a Variable a Vairable so that I have a number to subtract by so the Map gets smaller
local num = 1 -- this should work... First time, lol

-- Making a function so that when the player clicks and the sword swings the Baseplate gets smaller...
--Five Studs

sword.?????:connect(function()
	-- Create new CFrame
	local newVector3 = Vector3.new(-8, 3, -94.5)
	local newCFrame = CFrame.new(newVector3 - 1)

	-- Overwrite red block's current CFrame with new CFrame
	base.CFrame = newCFrame
	-- Now we are using the Baseplate Variable to make the Map smaller by Five Studs
	wait(.5)-- because its going to be to fast.
end)

You can look into the Tool.Activated event.

Ok, so I have to do…

Sword.Activated:Connect(function()

          -- STUFF

end)

Correct?

Yep, that is how you do it. It works until you want to do like hold for swinging continuously, where you can use ContextActionService / UserInputService.

Im getting errors

Am I doing something Wrong?

Yeah, that isn’t how ContextActionService is to be used. Although are you sure you need it now? ContextActionService isn’t a event of tool instances, what it does is, it allows you to bind Player Input to some kind of function. Just try using .Activated for now, instead of implementing your own system with ContextActionService, if its not needed currently as Activated does nearly the same thing except it just Fires when the player clicks using the Tool equipped.

hmm, Alright.

Well… thanks for Trying.

Ill keep looking. :slight_smile:

Hi Matt. There’s a few problems with this code.

local sword = game.StarterPack.ClassicSword

The StarterPack is a container which is not associated with any particular player. When a player spawns, everything inside of the StarterPack is moved into the “Backpack” container under the Player instance.

Therefore, sword in this case would refer to the sword that is just lying around the StarterPack, and not related to any particular player! We’ll want to reference the sword in the Player’s Backpack instead.

Since we’ll be handling user input, we will need to use a LocalScript for this, which will run on an individual player’s computer (not the server).

You could put the LocalScript in StarterCharacterScripts, and reference the tool like this:

local player = game.Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
local sword = backpack:WaitForChild("Sword")

Or, you could just put the script in the Tool, and reference it a bit more easily:
local sword = script.Parent

Now that the reference to the sword is correct, we can use the Tool.Activated event to perform an action when the player clicks with the tool equipped.

sword.Activated:Connect(function()
    print("Clicked with the tool equipped!")
end)

Putting it all together, for a LocalScript inside the Sword:

local sword = script.Parent
sword.Activated:Connect(function()
    print("Clicked with the tool equipped!")
end)

Since this is a LocalScript, any changes you make, like trying to damage another player, won’t ‘replicate’ to other players. To do that, we’d need to use a RemoteEvent to tell the server to do something for us, but that’s getting a bit outside the scope

Im having a infinite yeild on this line of code.
local sword = backpack:WaitForChild("Sword")