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

What is wrong with my Script formatting?

Asked by 9 years ago

It opens up the Gui perfectly and all however I want it so when a player presses "e" and they ARE near the part.. it disables the Operation script located inside the part they are near.

Keybind function

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.KeyDown:connect(function(key)

if near and key == "e"  then
    interaction.Operation.Disabled = true
end

interaction is defined as the part they are near. However I can't put it in the function update(player) or my script will give me red syntax lines. And if I just leave the keybind function where it is in the script. If I do, the script says it forgot to close.

Keybind function must be in function update(player)

function update(player)
    -- We defined the table of "interactions" above
    local torso = player.Character.Torso
    local near = nil -- Near nothing.
    for _, interaction in pairs(interactions) do
        if (interaction.position - torso.Position).magnitude <= 3 then
            -- Within three studs of interaction's center
            near = interaction
        end
    end

    local playerGui = player.PlayerGui
    local hasGui = playerGui:FindFirstChild( interactionGui.Name )
    if near then
        -- I am near an interaction
        if not hasGui then
            interactionGui:Clone().Parent = playerGui
        end
    else
        -- I am NOT near ANY site
        if hasGui then
            hasGui:Destroy()
        end
    end
end



local mouse = game.Players.LocalPlayer:GetMouse()
mouse.KeyDown:connect(function(key)

if near and key == "e"  then
    interaction.Operation.Disabled = true
end





-- Update all players
function updateAll()
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.Character
            and player.Character:FindFirstChild("Torso")
            and player:FindFirstChild("PlayerGui") then
            update(player)
        end
    end
end

while wait(0.1) do
    updateAll()
end

Full script


local me = game:GetService("Players").LocalPlayer local mouse = me:GetMouse() -- REQUIRES: model and descendants of model can have :GetChildren() called -- on them. -- PURPOSE: Given a model, returns a list of all of the descendants of that model. -- [If result is specified, it will add the descendants of result to model.] function descendants( model, result ) result = result or {} -- No need to pass in result for _, child in pairs(model:GetChildren()) do table.insert(result, child) -- Much cleaner way to add to list descendants(child, result) end return result -- Return the accumulated children end -- PURPOSE: Returns whether or not this part is an interaction site. -- REQUIRES: part must be a ROBLOX object. function isInteraction( part ) return part:IsA("BasePart") and part:FindFirstChild("Interaction") and part.Interaction:IsA("StringValue") end local interactions = {} -- List of all interaction sites. local everything = descendants( workspace ) for _, object in pairs( everything ) do if isInteraction( object ) then table.insert( interactions, object ) end end -- Define the GUI we give to players local interactionGui = game.ReplicatedStorage.InteractionGui -- REQUIRES: player is a Player, they have a Character (with torso) and PlayerGui. -- PURPOSE: Shows or hides (by deleting/inserting) the InteractionGui -- based on whether or not they are close to any interaction site (in `interactions`) function update(player) -- We defined the table of "interactions" above local torso = player.Character.Torso local near = nil -- Near nothing. for _, interaction in pairs(interactions) do if (interaction.position - torso.Position).magnitude <= 3 then -- Within three studs of interaction's center near = interaction end end local playerGui = player.PlayerGui local hasGui = playerGui:FindFirstChild( interactionGui.Name ) if near then -- I am near an interaction if not hasGui then interactionGui:Clone().Parent = playerGui end else -- I am NOT near ANY site if hasGui then hasGui:Destroy() end end end local mouse = game.Players.LocalPlayer:GetMouse() mouse.KeyDown:connect(function(key) if near and key == "e" then interaction.Operation.Disabled = true end -- Update all players function updateAll() for _, player in pairs(game.Players:GetPlayers()) do if player.Character and player.Character:FindFirstChild("Torso") and player:FindFirstChild("PlayerGui") then update(player) end end end while wait(0.1) do updateAll() end

1 answer

Log in to vote
0
Answered by 9 years ago

It seems you didn't quite end the event function correctly. Also remember, anonymous functions require a specific amount of closing parenthesis at the end. To fix this, let's do:

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.KeyDown:connect(function(key)
if near and key == "e"  then
    near.Operation.Disabled = true --Since you set it to that.
end
end)

Source

http://wiki.roblox.com/index.php?title=Anonymous_function

Ad

Answer this question