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

How do I make sure a script in a tool only works when the tool is out?

I made a script I'm very pleased with that fires an orb from a wand tip to the mouse location, but I noticed it also works with other tools out and no tool at all. Can I get a hint or some guidance as to how I can make sure that this script only runs when this tool is out? Thanks Immediately below is the hierarchy StarterWand(Tool) > Main(LocalScript) > MAIN(Script) and UseWand(RemoteEvent) StarterWand> Handle(Part) and Stick(Part)

Main:

wait (1.0)
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

function Clicked (mouse)
	script.UseWand:FireServer()
end

function ConnectFunction (mouse)
	Mouse.Button1Down:connect(Clicked)
end

script.Parent.Equipped:Connect(ConnectFunction)

MAIN:

local mouse = game.Players.LocalPlayer:GetMouse() -- allows us to see what the mouse is doing
local debounce = false
script.Parent.UseWand.OnServerEvent:Connect(function(Player) -- create a function for when the tool is used. No name needed for the function since it is called upon on a server event.
	if debounce == false then
		debounce = true
		wait(.5)
		local Orb = game.ReplicatedStorage.Small:Clone() -- create a part called orb that is exactly like the part in replicated storage called small
		Orb.CFrame = script.Parent.Parent.Handle.CFrame*CFrame.new(0 ,1.6, 0) -- give the orb a reference point to spawn from, this is at the origin of the handle
		Orb.Parent = game.Workspace -- make sure the part's parent is workspace so it is in the workspace
		local BV = Instance.new("BodyVelocity", Orb) -- give it a body velocity so that it may move
		BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge) -- Give the body velocity a maximum force to not exceed
		BV.Velocity = (mouse.Hit.p - Orb.Position).unit * 65 -- velocity is position and speed so I have it a position, the players mouse and a speed of 65
	
		local alreadytouched = false
		Orb.Touched:connect(function(hit)
			if alreadytouched == false then
				local human = hit.Parent:FindFirstChild("Humanoid")
				if human ~= nil and human.Parent ~= Player.Character then
					human:TakeDamage(10)
					alreadytouched = true
				end
			end
		end)
		wait (.5)
	debounce = false
	end
end)

Please and thank you, I'm still learning :D

4 comments

0

Please use a "Code Block" for each of the scripts.

0

And... what you meant is... to detect if the player has selected the tool?

0

Sorry I updated it. I meant that currently the script is shooting the orb when I left click no matter what the player is holding and I want it to only shoot when they have this tool in their hands.

0

I think I get it, I had this problem when I made some guns. So, I made a bool value, so I made the script below. youll have to clean it up.

1 answer

wait (0.25)
local tool = script.Parent
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local Active = false

function Clicked (mouse)
	if Active == true then
  		script.UseWand:FireServer()
	end
end

tool.Equipped:Connect(ConnectFunction)
	Active = true
	Mouse.Button1Down:Connect(function(Clicked)
		Clicked()
	end)
end)


tool.UnEquipped:Connect(function()
	Active = false
end)

2 comments

0

I have been trying to tweak this and nothing is helping.

0

I updated the local script, check your serverscript.

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