Local script not working in game

Im using a local script under PlayerScripts to check for guns a player has and scale it down since the guns in my game are huge for purposes. some reason the script doesnt run in game consistently but does in studio. anyone know why it only runs once in a blue moon?

SCRIPT:

task.wait()

local player = game.Players.LocalPlayer
local mycharacter = player.CharacterAdded:Wait()

print("character loaded")

task.spawn(function() -- CHECK FOR EXISTING SCALES
	for i, characters in pairs(game.Workspace:GetChildren()) do
		task.spawn(function()
			if characters == mycharacter then return end
			if not game.Players:GetPlayerFromCharacter(characters) then return end
			if characters:FindFirstChild("Humanoid") then
				print("character scallingg")

				local GV = characters:WaitForChild("GunInstance").Value
				local Held = GV:WaitForChild("HeldOV").Value
				local unHeld = GV:WaitForChild("UnHeldOV").Value

				if Held.Part0 ~= characters["Right Arm"] then
					Held.Part0 = characters["Right Arm"]
				end
				
				task.wait(.1)

				if GV:GetScale(GV:GetAttribute("truescale")) ~= GV:GetAttribute("truescale") then
					GV:ScaleTo(GV:GetAttribute("truescale"))
				end
				
				task.wait(.1)
				
				if Held.Part0 == characters["Right Arm"] then -- if holding
				else
					Held.Part0 = nil
					unHeld.Part0 = characters["Torso"]
				end

			end
		end)
	end
end)

task.spawn(function()
	local GunValue = mycharacter:WaitForChild("GunInstance")
	local Gun = GunValue.Value
	local Held = Gun:WaitForChild("HeldOV").Value
	local Unheld = Gun:WaitForChild("UnHeldOV").Value
	Held.Part0 = mycharacter["Right Arm"]
	task.wait(.1)
	if Gun:GetAttribute("truescale") then
		Gun:ScaleTo(Gun:GetAttribute("truescale"))
	end
	task.wait(.1)
	Held.Part0 = nil
	Unheld.Part0 = mycharacter.Torso
end)


local function Scale(Child)
	if not Child:FindFirstChild("Humanoid") then return end
	if Child:FindFirstChild("Humanoid") then
		local ACharacter = Child
		if ACharacter:WaitForChild("GunInstance") then
			local TheirGun = ACharacter:WaitForChild("GunInstance")
			if TheirGun.Value:GetAttribute("truescale") then
				local heldinstance = TheirGun.Value:WaitForChild("HeldOV")
				local unheldinstance = TheirGun.Value:WaitForChild("UnHeldOV")
				heldinstance.Value.Part0 = ACharacter["Right Arm"]
				task.wait(.1)
				TheirGun.Value:ScaleTo(TheirGun.Value:GetAttribute("truescale"))
				task.wait(.1)
				heldinstance.Value.Part0 = nil
				unheldinstance.Value.Part0 = ACharacter.Torso

			end
		end
	end
end

game.Workspace.ChildAdded:Connect(function(Child)
	if Child:FindFirstChild("Humanoid") then
		if Child == mycharacter then
			return
		end
		Scale(Child)
	end
end)

It’s possible that line 3 is the problem.

I suspect that the character is being added prior to the task.wait() concluding, meaning that

player.CharacterAdded:Wait()

yields forever basically.

Consider replacing line 3 with:

local mycharacter = player.Character or player.CharacterAdded:Wait()

thanks fixed it, prob was warning infinite yield but didn’t see it

The infinite yield warning only appears when using :WaitForChild(), so there wouldn’t have been a notice.

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