Script weirdly doesn't activate in the certain conditions

Hi. I have a piece of code that should work to my knowledge however it doesn’t. A quick rundown of the code: Tries to set a script’s Disabled property to false when the tool is parented to the Workspace.

Code:

local Tool = script.Parent

if Tool.Parent == "Workspace" then
	Tool.Handle.ImpactSoundsForSingleObjects.Disabled = false
	
else
	Tool.Handle.ImpactSoundsForSingleObjects.Disabled = true
end

Edit: Forgot to mention, the code doesn’t work at all. It doesn’t do freaky stuff like setting the script to true while the parent is something else from Workspace, just doesn’t work.

Try this. :thinking:

local Tool = script.Parent

if Tool.Parent == game.Workspace then
	Tool.Handle.ImpactSoundsForSingleObjects.Disabled = false
	
else
	Tool.Handle.ImpactSoundsForSingleObjects.Disabled = true
end

Please read the Reply below for more information. :point_down:

2 Likes

Parent is an instance pointer property, not a string.

So you compare the tool’s parent (which is presumed to be the workspace service, which is an instance) to of type string. Obviously, those two types will not be the same.

Instance pointer meaning it’s a reference to some instance type, not an actual object that’s stored as the parent, if that makes any sense at all.

Hopefully this clears the air a bit. @Escape_Everything gave you the functional expression.

2 Likes

Thanks for that explanation!!!

1 Like

Ah, thanks so much man! Probably should’ve used game.Workspace in the start, though. :sweat_smile:

1 Like

You could use the global variable workspace too, which is a direct reference to the Workspace service.

It’s more efficient too (albeit the difference is negligible).

task.wait(1)
local t1 = os.clock()
print(workspace)
local t2 = os.clock()
print(game.Workspace)
local t3 = os.clock()
print(t2 - t1)
print(t3 - t1)

image

Update: I was on the phone when I accepted the solution however didn’t test out @Escape_Everything 's solution. Sadly, it didn’t work however for any other people experiencing an issue of this sorts, here is my code!

local Tool = script.Parent

Tool:GetPropertyChangedSignal("Parent"):Connect(function()
	if Tool.Parent == workspace then
		Tool.Handle.ImpactSoundsForSingleObjects.Disabled = false
	else
		Tool.Handle.ImpactSoundsForSingleObjects.Disabled = true
	end
end)

Wanna thank everybody who replied and tried to help me! (@Forummer @C_Sharper @Escape_Everything )

When tools are equipped, they get thrown into the player’s character model, so technically they’re not members of workspace.

That said, if you’re trying to determine if the tool is currently being held, you can do:

if Tool.Parent.Name ~= "Backpack" then
--//Tool is currently being held

else
--//Tool is put away
end