I’m trying to create a system where if a script detects a RopeConstraint in the HumanoidRootPart, it will turn a GUI frame green, and if not, red. But, I keep getting the error, Workspace.godtrek102playz.LocalScript:6: attempt to index nil with 'ScreenGui'
Here’s the code.
rope = script.Parent.HumanoidRootPart:FindFirstChild("RopeConstraint") name = script.Parent.Name if rope then game.Players.name.PlayerGui.ScreenGui.Frame.BackgroundColor3 = Color3.fromRGB(255,0,0) elseif not rope then game.Players.name.PlayerGui.ScreenGui.Frame.BackgroundColor3 = Color3.fromRGB(0,255,0) end
Use game:GetService() rather than using the dot operator.
Example:
Use game:GetService("Players") instead of game.Players. This allows to eliminate possible errors and helps you to access the services even if they are renamed.
I see you using the Frame object multiple times, so declare it as a variable.
Try using ternary operations if you understand them.
local rope = script.Parent.HumanoidRootPart:FindFirstChild("RopeConstraint")
local frame = game:GetService("Players").LocalPlayer.PlayerGui.ScreenGui.Frame
frame.BackgroundColor3 = rope and Color3.fromRGB(255, 0, 0) or Color3.fromRGB(0, 255, 0)
I tried the LocalPlayer fix to replace game.Players.name.PlayerGui.ScreenGui.Frame.BackgroundColor3 =
but all it does is just turn the icon green when it’s supposed to be red and doesn’t change when a RopeConstraint is added to the HumanoidRootPart.
rope = script.Parent.HumanoidRootPart:FindFirstChild("RopeConstraint") local player = game.Players:GetPlayerFromCharacter() local plrGui = player.PlayerGui if rope then plrGui.ScreenGui.Frame.BackgroundColor3 = Color3.fromRGB(255,0,0) elseif not rope then plrGui.PlayerGui.ScreenGui.Frame.BackgroundColor3 = Color3.fromRGB(0,255,0) end
What should I put in place of “character”? It gives me the error “Unknown global ‘character’” If I leave it blank, the script doesn’t work.
You can get the Player using the :GetPlayerFromCharacter function. Here is an example:
local character = script.Parent
local player = game:GetService("Players"):GetPlayerFromCharacter(character)
local rope = script.Parent.HumanoidRootPart:FindFirstChild("RopeConstraint")
local RopeName = script.Parent.Name
if player then
if rope then
player.PlayerGui.ScreenGui.Frame.BackgroundColor3 = Color3.fromRGB(255,0,0)
else
game.Players.name.PlayerGui.ScreenGui.Frame.BackgroundColor3 = Color3.fromRGB(0,255,0)
end
end
The function above gets the player and all the folders involving the player using the API GetPlayerFromCharacter. You can read more about the API here.
local character = script.Parent local player = game:GetService("Players"):GetPlayerFromCharacter(character) local rope = script.Parent.HumanoidRootPart:FindFirstChild("RopeConstraint") if player then if rope then player.PlayerGui.ScreenGui.Frame.BackgroundColor3 = Color3.fromRGB(255,0,0) else player.PlayerGui.ScreenGui.Frame.BackgroundColor3 = Color3.fromRGB(0,255,0) end end
Again, this just turns the frame green and no change in color is made when a RopeConstraint gets attached. I might just post the .rbxm file with all the components since I’m quite lost here.
If you want the UI to be updated upon something being changed, in your case a RopeConstraint added as a child of HumanoidRootPart, you need to listen to that change via an event and then update the UI.
Try:
local frame = game:GetService("Players").LocalPlayer.PlayerGui.ScreenGui.Frame
local hrp = script.Parent.HumanoidRootPart
local function updateUI()
local rope = hrp:FindFirstChild("RopeConstraint")
frame.BackgroundColor3 = rope and Color3.fromRGB(255, 0, 0) or Color3.fromRGB(0, 255, 0)
end
hrp.ChildAdded:Connect(updateUI)
updateUI()