Game script timeout

My script below is timing out, I am not sure why any help?

script.Ammo.Changed:Connect(function()
	if script.Ammo.Value == 0 then
	wait()
    reload()
	end
	end)
1 Like

Can you please show us the reload function, it’ll help out significantly.

function reload()

r = true

local reload = Instance.new("Animation")

reload.AnimationId = "http://www.roblox.com/asset/?id=4332318329"

local animationTrack2 = game.workspace.GunNPC.Humanoid:LoadAnimation(reload)

animationTrack2:Play()

script.Parent.RightHand.ReloadSound:Play()

wait(3)

script.Ammo.Value = 12

r = false

end

game.workspace should either be workspace or game.Workspace, the capitalization matters.

This didn’t fix the issue, but thank you.

Are any errors printed aside from a timeout message, and if so, at what line?

No errors besides the timeout.

Can you walk us through the process? For example:

  1. Does the character ever play the animation before the script times out?
  2. Is there more to the script then just the reload function that might play a role in time outs? (i.e. infinite loops)
  3. What line does the timeout happen on, if it is listed?
  4. Is there anything that changes the value Ammo besides this function and do you have a debounce to handle multiple reloads? (If the value changes too much and all those threads are created, it will overload the game causing a timeout)

If none of these prove sufficient, notify me please.

You should be checking the the new value is updated not the current one. The changed function has an argument with the new value.

If what you’re saying is (and correct me if I’m wrong) that he should change the function to this:

script.Ammo.Changed:Connect(function(newVal)
    if newVal == 0 then
        wait()
        reload()
    end
end)

it won’t matter as the value is checked after the change occurs, meaning it is updated anyways.

That function would error because the parameter of a .Changed argument is a string value, what he could do instead is something along the lines of;

script.Ammo:GetPropertyChangedSignal("Value"):Connect(function()
    if script.Ammo.Value == 0 then
        wait()
        reload()
    end
end)

Though I’m not sure if it is so much an error of the calling of the function itself so much as it is an issue w/ animations loading.

It appears you have set boolean values to a variable named r.

…that variable isn’t tied to a loop elsewhere in your script, is it?

I’m assuming the “r” variable is used elsewhere in the script to signify that the player is reloading and to not allow things such as firing the weapon.

I think it might be an issue with infinite yielding when you stop the test server. Otherwise it’s somewhere else in your code. I don’t know if the problem described in the bug report happens with waiting in other places than BindToClose. I’m not sure if the fix they were talking about was pushed into today’s patch. If it wasn’t then it should be fixed within the week

No, that function wouldn’t error. ValueObjects have a specially designed version of Changed not inherited from Instance that fires with the new value of the ValueObject. Changed on any other instance fires with a string which determines which property changed.

Don’t use GetPropertyChangedSignal for ValueObjects, it’s unnecessary (and slower, albeit negligibly) than directly connecting to the given event.

There’s an oopsie with that you should check for the boolean whether it is reloading or not.

script.Ammo.Changed:Connect(function()
	if not r and script.Ammo.Value == 0 then
		reload()
	end
end)
1 Like