Why my prints run multiple times?

Same question as title says. Everytime I add new print() it runs 3 times for some reason. Any idea whats the cause? Does that mean my script runs 3 times?:

Local-Script located in StarterCharacterScripts

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local RunService = game:GetService('RunService')
local Player = game:GetService('Players').LocalPlayer
local Character = Player.Character

local ProfileBars = Player.PlayerGui.ProfileWindow.Holder.StatsHolder

local HealthBar = ProfileBars.HealthBarHolder.BarBackground.HealthBar
local ManaBar = ProfileBars.ManaBarHolder.BarBackground.ManaBar
local StaminaBar = ProfileBars.StaminaBar

local HValue = ProfileBars.HealthBarHolder.HealthNum
local MValue = ProfileBars.ManaBarHolder.ManaNum

-- those prints im talking about
print(Character:GetAttribute('CurrentHealth'), Player)
print(Character:GetAttribute('CurrentMana'), Player)

function getHealth()
	HValue.Text = Character:GetAttribute('CurrentHealth').."/"..(Character:WaitForChild('BaseStats'):GetAttribute('Health') + Character:WaitForChild('Stats'):GetAttribute('Endurance')/2)
end
function getMana()
	MValue.Text = Character:GetAttribute('CurrentMana').."/"..(Character:WaitForChild('BaseStats'):GetAttribute('Mana') + Character:WaitForChild('Stats'):GetAttribute('Inteligence'))
end
function getEverything()
	getHealth()
	getMana()
end

getEverything()
Character.AttributeChanged:Connect(getEverything)
Character:WaitForChild('Stats').AttributeChanged:Connect(getEverything)

ReplicatedStorage.Events.UpdateHealth:FireServer(Player)

Attribute CurrentHealth and CurrentMana are created server-sided

Script located in ServerScriptService

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Character:SetAttribute('CurrentHealth', Character:WaitForChild('BaseStats'):GetAttribute('Health'))
		Character:SetAttribute('CurrentMana', Character:WaitForChild('BaseStats'):GetAttribute('Mana'))
		Character:SetAttribute('CurrentStamina', Character:WaitForChild('BaseStats'):GetAttribute('Stamina'))
	end)
end)

AT THE TIME WRITING THE POST: I’ve noticed that after leaving only print() statements it still prints 3 times

image

Have you tried moving the script to StartPlayerScripts?

Now it works as it should! But why it made the difference?

Well, when you have it in StarterCharacterScripts, it generates new one every time your character loaded, while when you put it into StarterPlayerScripts, it is only loaded when player joins.
So that might cause some issues.

This makes sense, but it means my character loads multiple times?

Try creating new local script with only print in it in StarterCharacterScripts and see. If it prints 3 times then yeah.

image
So there is an issue with character loading. Can StarterCharacter cause such problems?

I tried putting in StarterCharacter, but I did not get multiple prints. I saw that you have health and mana and stuff, so I guess you’re making combat game, make sure none of the scripts respawn player (load character) for no reason. Also try respawning and see if its only when you join, or every time you respawn.

Even when I’ve removed StarterCharacter im getting those multiplied prints. Not a single script of mine have any respawn functions nor planning to add any soon. Checked if the same happens after character reset and nothing has changed. Also, I don’t think my health and mana stuff has something to do with it, because at the time of me writing this I have them disabled.

I’m gonna check if any other scripts are affected by this problem.

EDIT: Every script in StarterCharacterScript runs 3 times

Every time or only at the start ?

SOLUTION TO MY PROBLEM:
I have Viewport frame inside my GUIs that clones Player.Character to it for preview purposes. It clones everything including scripts. That means script inside my character and inside my cloned character is running. The best part about it is that my CLONE is generating ANOTHER CLONE of it for some reason. Thats why its running triple prints. I just have to delete every script inside my cloned character when clone is being created.

EDIT: I’ve found out about it after doing print(script) and checking its source

It was doing it every time character was loading but it’s fixed now. Thank you for the help!

1 Like

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