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

Script for custom NPC interaction not working, giving error in relation to data table?

Asked by 6 years ago

I have created a GUI that is enabled when a player clicks on a NPC. The click action activates a script that communicates with the client through a Remote Function.

The serverside script is as follows (it is located below a clickdetector within the NPC model and the remote Function is called "DialogEvent" located in the Replicated Storage):

-- Server Side Script
local detector = script.Parent
local Players = game:GetService("Players")
local NextLabelVisible = false
local nameLabel = ""
local ImageLabel = ""
local TextLabel = ""
local Choice1 = ""
local Choice2 = ""
local Choice2Active = false
local Cancel = ""
local UserId = 0
local DialogEvent = game.ReplicatedStorage:WaitForChild("DialogEvent")
local data = {UserID,nameLabel,TextLabel,NextLabelVisible,Choice1,Choice2Active,Choice2,Cancel}
local Answer = 9
local NPCID = 265276480

-- first dialog
function WelcomeDialog(Player)
UserID = Player.UserId
nameLabel = Player.Name .. ":"
TextLabel = "Heey Alia_h, how are you?"
NextLabelVisible = true 
Choice1 = ""
Choice2Active = false
Choice2 = ""
Cancel = "Sorry, Got to go"
DialogEvent:InvokeClient(Player,data)
return Answer
end

--- second Dialog
function secondDialog(Player)
UserID = NPCID
nameLabel = script.Parent.Parent.Name .. ":"
TextLabel = "I am fine, How do you like my art?"
NextLabelVisible = false
Choice1 = "It is beautiful"
Choice2Active = true
Choice2 = "Not my taste but beautiful"
Cancel = "Sorry, Got to go"
DialogEvent:InvokeClient(Player,data)
return Answer
end

-- Option One + Two
function SecondOneDialog(Player)
UserID =NPCID
nameLabel = script.Parent.Parent.Name .. ":"
TextLabel = "Thank you!"
NextLabelVisible = true
Choice1 = ""
Choice2Active = false
Choice2 = ""
Cancel = "..."
DialogEvent:InvokeClient(Player,data)
end

--Cancel Dialog
function CancelDialog(Player)
UserID =NPCID
nameLabel = script.Parent.Parent.Name .. ":"
TextLabel = "Oh, See you around!"
NextLabelVisible = true
Choice1 = ""
Choice2Active = false
Choice2 = ""
Cancel = "..."
DialogEvent:InvokeClient(Player,data)
end


-- main structure Answer = 0: Next; = 1: Option1; = 2: Option2; = 3: Cancel
function DialogTree(Player)
WelcomeDialog(Player)
-- only 0 and 3 accepted
    if Answer == 0 then
    secondDialog(Player)
    -- only 1, 2 & 3 accepted
        if Answer == 0 then
            secondDialog(Player)
        elseif Answer == 1 then
            SecondOneDialog(Player)
        elseif  Answer == 2 then
            SecondOneDialog(Player)
        elseif Answer == 3 then
            CancelDialog(Player)
        else
            print("error: option not found")
        end
    elseif Answer == 1 then
    WelcomeDialog(Player)
    elseif Answer == 2 then
        WelcomeDialog(Player)
    elseif Answer == 3 then
        CancelDialog(Player)
    else 
        print("error: option not found")
    end

end

--detector event
detector.MouseClick:connect(DialogTree)

the client side code is located in: startergui-> DialogGUI->DialogFrame-->LocalScript

the Script looks as follows:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DialogEvent = ReplicatedStorage:WaitForChild("DialogEvent")
local PlayerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local menu = game.Players.LocalPlayer.PlayerGui.Menu
local GUI = script.Parent.Parent.Parent
local Inputservice = game:GetService("UserInputService")
local Answer = 9
local InputCorrect = false
function OptionOneClicked()
    Answer = 1
    GUI.Enabled = false
    menu = true
    return Answer
end

function OptionTwoClicked()
    Answer = 2
    GUI.Enabled = false
    menu = true
    return Answer
end

function OptionCancelClicked()
    Answer = 3
    GUI.Enabled = false
    menu = true
    return Answer
end

function optionNextClicked(InputObject, gameProcessedEvent)
    if  InputObject.KeyCode == Enum.KeyCode.Return and not gameProcessedEvent then
    Answer = 0
    InputCorrect = true
    GUI.Enabled = false
    menu = true
    return Answer,InputCorrect
    end
end

--data structure: {UserID,nameLabel,TextLabel,NextLabelVisible,Choice1,Choice2Active,Choice2,Cancel}
function GUISetup(data)
script.Parent.Parent.Enabled = true
menu.Enabled = false

script.Parent.ImageLabel.Image = "https://www.roblox.com/bust-thumbnail/image?userId=" .. data[1] .. "&width=420&height=420&format=png"
script.Parent.NameLabel.Text = data[2]
script.Parent.SpeechLabel = data[3]
script.Parent.NextLabel.Active = data[4]
script.Parent.OptionOne.Text = data[5]
script.Parent.OptionTwo.Active = data[6]
script.Parent.OptionTwo.Text = data[7]
script.Parent.OptionCancel.Text = data[8]
if data[4] ~= true then
    script.parent.OptionOne.MouseButton1Click:connect(OptionOneClicked)
    script.Parent.OptionTwo.MouseButton1Click:connect(OptionTwoClicked)
    else
    script.Parent.OptionOne.Active = false
    script.Parent.OptionTwo.Active = false
    end
script.Parent.OptionCancel.MouseButton1Click:connect(OptionCancelClicked)
    while InputCorrect == false do
        Inputservice.InputBegan:connect(optionNextClicked)
        wait(0.1)
    end
return Answer
end

DialogEvent.OnClientInvoke = GUISetup

The error I get is that it can nog concatenate the script.Parent.ImageLabel.Image adress because field ? is a nil value… Any idea why this occurs? am I calling the data table entry wrong?

0
I am thinking I am filling the table wrong. is it allowed to fill the table 'data' this way (see server side script) Rubenske1 89 — 6y

Answer this question