My localScript is running twice

I am having this very weird issue where my localscript will run twice from the same instance, but output from the second parallel run of the script isn’t attributed to the script, only “Studio”.

image
These both appear to be coming from the same instance, the problem is that one gets wrong values from calls to instance:GetAttribute(), besides the fact that it shouldn’t exist in the first place.

Here is my code, placed in starterCharacterScripts:

local Player = game.Players.LocalPlayer
local CollectionService = game:GetService("CollectionService")
local last_refill = 0
local refill_interval = 5
local i = 0

function set_water(new)
	script.Parent:SetAttribute("Water" , new)
end

function get_water()
	return script.Parent:GetAttribute("Water")
end

function change_water(amount)
	set_water(get_water() + amount)
end

set_water(100)

script.Parent:GetAttributeChangedSignal("Water"):Connect(function()
	game.Players.LocalPlayer.PlayerGui.MainGui.HydrationFrame.Gradient.Size = UDim2.fromScale(math.max(get_water() , 0) / 100, 1)
end)


while script.Parent do

	while get_water() > 0 do
		task.wait(0.1)
		change_water(-1)
		print(get_water())
		if not script.Parent:IsA("Model") then break end
	end
	set_water(0)
	script.Parent.Humanoid.WalkSpeed *= 0.5
	while get_water() <= 0 do
		script.Parent.Humanoid.Health -= 2
		task.wait(1)
		if not script.Parent:IsA("Model") then break end
	end
	script.Parent.Humanoid.WalkSpeed *= 2
end

warn(script.Parent)

You can try removing line 49 on HydrationHandler and see if there is only one or two prints.

Do you have two of the same local scripts on accident?

Another solution may be deleting certain lines of code(temporarily) to narrow down the problem. If you delete a line and see that it doesn’t print 84 then you know that line may be causing the problem.

There are two prints from the very first line, the code is running twice is in parallel so one version can’t be accessed in any way.

So you want it to stop running parallel. Did you enable a setting that made it do this or did it happen randomly?

It just happens randomly for no apparent reason, worth noting it also only happens in studio.

Is it a Script with Client RunContext or is it just a Local Script?

It will be called twice and only when it is instanced. Since the Studio creates the LocalScript and parents it to the player, that means that this is called by Studio as it is created:

set_water(100)

which invokes

script.Parent:GetAttributeChangedSignal("Water")

the next execution is in your loop (more than likely Client side)

while script.Parent do
	while get_water() > 0 do
		task.wait(0.1)
		change_water(-1) -- this calls set_water() which invokes the attribute change again a heartbeat later
	end
...

Why not just set the attribute “Water” to 100 in the workspace tree settings and get rid of the out-of-scope call set_water(100)

The dual call to if not script.Parent:IsA("Model") then break end is somewhat troublesome, if the first check passes, then so too could the second one, however, you still reference Humanoid in between the two while loops when the first check fails. If the scripts parent is not a model then any logic on any descendants of the model should cease. When the player model is destroyed so too will the “Water” attribute and the LocalScript we have in question.