Value will not refresh when tool equipped

I have a tool and it loads values each time it is equipped.

I want the values to refresh but it doesn’t work.

EXAMPLE:

local function activateTool()
local exampleValue = script.Parent:WaitForChild("Example").Value
-- functions
end

local connectExample = script.Parent.Equipped:Connect(function() activateTool() end)

script.Parent.Unequipped:Connect(function()
connectExample:Disconnect()
end)

The script works fine, except it won’t refresh the value.

Anyone know why?

Just a note: If I don’t set the script up as a connect/disconnect then the values will multiply. So if the value = 1 then each time the tool is equipped the value will increate by 1 - which I don’t want to happen.

1 Like

Show your full code, everything seems fine here but I suppose you have more code in the function that might be causing this issue.

1 Like

It is 500 lines of code and I really don’t want to post all that.

2 Likes

I added a print command to the example script.
It only prints once, no matter how many times I equip the example tool.

local function activateTool()
local exampleValue = script.Parent:WaitForChild("Example").Value
print("Tool Equipped")
end

local connectExample = script.Parent.Equipped:Connect(function() activateTool() end)

script.Parent.Unequipped:Connect(function()
connectExample:Disconnect()
end)

Example.rbxl (38.3 KB)

1 Like

There’s your answer, don’t know how I didn’t notice it. You disconnect the function from the event so the event’s function will never run again. Try doing this:

local function activateTool()
    local exampleValue = script.Parent:WaitForChild("Example").Value
    print("Tool Equipped")
end

connectExample = script.Parent.Equipped:Connect(activateTool)

script.Parent.Unequipped:Connect(function()
if connectExample then
    connectExample:Disconnect()
end
end)

I also cleared the code up for you cause I hate seeing unorganized code, coming from a person with OCD.

1 Like

Thank you. I can appreciate OCD. I had it for a long time.

Unfortunately, your adjustments did not change the outcome. It still will only print one time.

Then I don’t think you even need to disconnect the event. Try without disconnecting it at all.

I had it without a connect/disconnect originally, but the values kept duplicating.

The 500 lines of code also loads several hundred lines of module scripts. I’m not that adept at programming. About 90% of the code comes from FastCast.

His code works fine if I just use it as provided, but I need to add custom values for the tools settings and need my values loaded.

The only way I could do that was to wrap his coding inside a function that loads my values before his.

I did notice that if I remove the connect/disconnect from the example then it will print every time the tool is equipped.

I don’t have the skill to dig through hundreds of lines of code.

Does anyone know how to have custom variables that load when a player equips a tool?

I need variables in a script to be filled in after the tool is equipped because a script without defined variables will crash when it tries to find variables that don’t yet exist.

I put the variables inside an activeTool() function so they would only populate when the tool is equipped, but that is causing too many problems with the connected FastCast functions.

Problem solved.

I created a new script and placed it into the tool:

script.Parent.Equipped:Connect(function() script.Parent.ToolScript.Enabled = true end)
script.Parent.Unequipped:Connect(function() script.Parent.ToolScript.Enabled = false end)

It toggles the main script on and off so the variables do not need to be defined until the tool is equipped. That way I can remove the activeTool() wrapper function that was causing all the problems.

Thank you Darkness for all your input.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.