Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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

0
Please use a "Code Block" for each of the scripts. AIphanium 124 — 5y
0
And... what you meant is... to detect if the player has selected the tool? AIphanium 124 — 5y
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. DentonCPerry 0 — 5y
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. Decimaprism 65 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
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)

0
I have been trying to tweak this and nothing is helping. DentonCPerry 0 — 5y
0
I updated the local script, check your serverscript. Decimaprism 65 — 5y
Ad

Answer this question