Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to reference a part using objectvalue in a serverscript?

Asked by 3 years ago

This is all in a normal Script, not a local script.

Screenshot 1 Screenshot 2 Screenshot 3

The helicopter is referenced using an Objectvalue, however, when attempting to reference it in the script it returns as nil, I am guessing because the server can not see it. However, I do not know any practical way around this.

local attached = script.Parent.Attached
local inlet = script.Parent.InletStatus
local main = script.Parent.MainStatus
local Return = script.Parent.ReturnStatus
local helicopter = script.Parent.helicopter.Value

function refuel()
    print("refuel")
    local fuel = helicopter.Parent.Parent.Stats.Fuel

    while fuel.Value <100 and inlet == true and main == true and attached == true do
        fuel.Value = fuel.Value + 2

        wait(1 / 5)
    end
end

inlet.Changed:Connect(function()
    print("inlet")
    refuel()
end)

main.Changed:Connect(function()
    print("main")
    refuel()
end)

attached.Changed:Connect(function()
    print("attached")
    refuel()
end)

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Hello. Try using :WaitForChild() on the helicopter. If that doesn't work, make sure that the helicopter value is set, as I saw on your screenshot that it wasn't set. Remember that scripts run instantly and don't wait for something to happen unless you tell it to. Also, try using a repeat wait() until the helicopter value is set.

local attached = script.Parent.Attached
local inlet = script.Parent.InletStatus
local main = script.Parent.MainStatus
local Return = script.Parent.ReturnStatus
local helicopter

repeat 
    wait() 
until script.Parent:WaitForChild("helicopter").Value

helicopter = script.Parent.helicopter.Value

local function refuel()
    print("refuel")
    local fuel = helicopter.Parent.Parent.Stats.Fuel

    while fuel.Value <100 and inlet == true and main == true and attached == true do
        fuel.Value = fuel.Value + 2

        wait(1 / 5)
    end
end

inlet.Changed:Connect(function()
    print("inlet")
    refuel()
end)

main.Changed:Connect(function()
    print("main")
    refuel()
end)

attached.Changed:Connect(function()
    print("attached")
    refuel()
end)
Ad
Log in to vote
-1
Answered by 3 years ago

Im guessing that you need to add another value, because values are called values and aren't actually getting the values inside of them.

local attached = script.Parent.Attached
local inlet = script.Parent.InletStatus
local main = script.Parent.MainStatus
local Return = script.Parent.ReturnStatus
local helicopter = script.Parent.helicopter.Value.Value

function refuel()
    print("refuel")
    local fuel = helicopter.Parent.Parent.Stats.Fuel

    while fuel.Value <100 and inlet == true and main == true and attached == true do
        fuel.Value = fuel.Value + 2

        wait(1 / 5)
    end
end

inlet.Changed:Connect(function()
    print("inlet")
    refuel()
end)

main.Changed:Connect(function()
    print("main")
    refuel()
end)

attached.Changed:Connect(function()
    print("attached")
    refuel()
end)

Answer this question