#Why wouldn't this work?

44 messages · Page 1 of 1 (latest)

young breach
#

I'm trying to make a script where if you equip a tool, you can click on someone and their name will show, but it does it even when the tool isn't equipped

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local tool = script.Parent

local mouse = plr:GetMouse()
local debounce = true

if not tool.Equipped then return end

mouse.Button1Down:Connect(function()
    local target = mouse.Target
    if debounce then
        debounce = false
        if target.Parent:FindFirstChild("Humanoid") and target.Parent ~= game.Workspace then
            print(target.Parent.Name)
        end
        debounce = true
    end
end)
#

I don't understand why I can't just say "if not tool.Equipped then return end"

#

Is it because the script only runs once?

#

'tried to put the check inside of the function, didn't change anything

fast wharfBOT
#

studio** You are now Level 12! **studio

lavish shore
#

it does works 🙂

#

that code will run exactly once

#

when you join the game

#

and that's it

#

then the script dies

young breach
#

Yeah but when I join the game, the tool isn't equipped

lavish shore
#

so it returns and dies

#

yeah

young breach
#

Should I run the scripts outside of the tool

lavish shore
#

that made not a whole lot of sense

young breach
#

What didn't?

lavish shore
#

how would that change it

#

I just told you your script works, your logic is the one failing here

young breach
#

Well in startercharacterscripts

#

or in serverscriptservice

lavish shore
#

it's doing exactly what you are telling it to do

#

you are just not aware

young breach
#

Wait

#

Can you tell me

#

Why whenever the tool isn't equipped, when the game starts

#

I can still click on people

#

and have their name printed

#

Hope that makes enough sense, my mind is roasted atm

lavish shore
#

because your if statement is in the global scope of the script

#

again 2nd time, your if statement will be evaluated once

#

and that's it

young breach
#

So I should make it a loop

lavish shore
#

nope

young breach
#

shit

lavish shore
#

that would yield the thread

#

annd do nothing

young breach
#

I've tried to make it a function instead, whenever the tool is equipped and you press your mouse button

lavish shore
#

mouse.Button1Down:Connect(function()
    local target = mouse.Target
    if debounce then
        debounce = false
        if target.Parent:FindFirstChild("Humanoid") and target.Parent ~= game.Workspace then
            print(target.Parent.Name)
        end
        debounce = true
    end
end)
#

ask inside the event

young breach
#

the function happens

#

inside the event?

#

wdym by that