2016-02-28 117 views
-2

我試圖製作一個GUI,如roblox教程here腳本未運行

它在我在工作室測試時有效,但不是當我點擊play game時加入我的角色。

local player = game.Players.LocalPlayer 
local unitFrame = script.Parent 

unitFrame.Avatar.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&username="..player.Name 
unitFrame.PlayerName.Text = player.Name 

local healthBar = unitFrame.HealthBarContainer.HealthBar 
player.CharacterAdded:connect(function(character) 
    local humanoid = character:WaitForChild('Humanoid') 
    humanoid.HealthChanged:connect(function(health) 
     local healthPercentage = health/character.Humanoid.MaxHealth 
     healthBar.Size = UDim2.new(healthPercentage, 0, 1, 0) 
    end) 
end) 


local StarterGui = game:GetService('StarterGui') 
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false) 

回答

0

確保腳本是localscript。在studio中測試時,腳本經常在工作室中工作,但不在服務器中工作。

要查看問題所在,請加入您的遊戲,然後單擊F9作爲命令提示符,並告訴我它以紅色表示,然後我們可以看到問題所在。

+0

是的,它是一個本地腳本。我已經按f9,並在服務器日誌中說,內容失敗,因爲HTTP 404(HTTP/1.1 301永久移動) - 謝謝 – AskingEverythingForCode

0

啓用HttpEnabled。 ROBLOX無法通過HTTP連接到其服務器以獲取您的角色。

輸入到這個命令吧:

game:GetService("HttpService").HttpEnabled = true 
0

不幸的是,它看起來像我做該教程時所做的監督。如果您查看本地日誌(而不是服務器日誌,因爲此代碼在LocalScript中運行),您將看到問題在第5行:unitFrame.PlayerName.Text = player.Name。日誌抱怨說它不認爲PlayerName是unitFrame的成員或子節點。

此問題來自複製滯後。當玩家第一次加入遊戲時,所有內容都從StarterGui複製到他們的PlayerGui中。但是,這並不是立即發生,也不是一次全部發生。這個LocalScript完全可以複製並在其他GUI元素複製之前開始執行。

在這種情況下,正確的解決方案是使用WaitForChild。這個函數產生直到子對象實際存在。你應該使用這兩種訪問阿凡達ImageLabel和PlayerName爲textLabel像這樣的時候:

unitFrame:WaitForChild("Avatar").Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&username=" .. player.Name 
unitFrame:WaitForChild("PlayerName").Text = player.Name 

請記住,你沒有這個,你訪問一個GUI內的孩子都必須這樣做。但是在初始化處理GUI的LocalScripts時建議這樣做,以便GUI元素有時間加載。

作爲一個方面說明,我會確保今天教程更新了正確的代碼。我爲錯誤表示歉意。