Why my script was triggered many times.
Players.PlayerAdded:Connect(function(player)
local HasTycoon = Instance.new("BoolValue", player)
HasTycoon.Name = "HasTycoon"
HasTycoon.Value = false
player.CharacterAdded:Connect(function(character)
local humanoid
repeat
humanoid = character:FindFirstChild("Humanoid")
task.wait()
until humanoid
local playerhealth = game.ReplicatedStorage.PlayerHealth:Clone()
playerhealth.Parent = character:WaitForChild("Head")
end)
end)
It’s a script in serverscriptservice.I used Find All to make sure only the script call “PlayerHealth”.So when a player added.The “PlayerHealth” will be clone to player’head.It’s a billboardgui and have script to change player’s health and name which displayed on player’head.
print("1111")
local players = game:GetService("Players")
local player = players:GetPlayerFromCharacter(script.Parent.Parent.Parent)
if player.HasTycoon.Value == true then
print("tr")
local colorp = Instance.new("Part")
colorp.BrickColor = player.TeamColor
local color = colorp.Color
colorp:Destroy()
script.Parent.NameGui.TextColor3 = color
script.Parent.NameGui.UIStroke.Color = color
script.Parent.NameGui.Text = player.Team.Name .. ":" .. player.Name
else
print("fa")
script.Parent.NameGui.Text = player.Name
end
game.ReplicatedStorage.HealthScreen.OnServerEvent:Connect(function(Player,hp,maxhp)
if Player == player then
game.TweenService:Create(script.Parent.Frame,TweenInfo.new(0.05,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,true),{Size = UDim2.fromScale(0.8,0.8)}):Play()
game.TweenService:Create(script.Parent.Frame.Bar,TweenInfo.new(0.1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out),{Size = UDim2.fromScale(math.clamp(hp/maxhp,0,1),1)}):Play()
script.Parent.Frame.TextLabel.Text = "Health" .. tostring(math.floor(hp)) .. "/" .. tostring((math.floor(maxhp)))
end
end)
player.HasTycoon.Changed:Connect(function()
print("change")
local colorp = Instance.new("Part")
colorp.BrickColor = player.TeamColor
local color = colorp.Color
colorp:Destroy()
script.Parent.NameGui.TextColor3 = color
script.Parent.NameGui.UIStroke.Color = color
script.Parent.NameGui.Text = player.Team.Name .. ":" .. player.Name
end)
It’s all script in “PlayerHealth”.HasTycoon will be set true when player get a tycoon.And the script will change the NameGui.Text.But it wont trigger when the value set to false(When player Rebirth),I also dont know wht.
The most important thing is when wait a random time.the script will trigger once.
(The above two lines are printed when the player joins)
I’m sure no another script call the script.So i dont know why it was triggered Repetitively.Any idea is commendable.
(In addition,the health bar was changed normally)
