#Help running a script in ServerScriptService via script in a part (with a variable value)

1 messages · Page 1 of 1 (latest)

solid moon
#

Hello! I am VERY new to coding in Roblox Studio, and what I need assistance with is having a part that is clickable, and when it is clicked, it runs a function that is in ServerScriptService with a certain value attached (could be a variable or like an argument for a function, idk, whatever works best!). I have coded before, so I know how coding works, just not how Roblox coding works and such. Thank you for your assistance! I also do not know what tag to put this as, hopefully this one is okay-

#

Basically, I would like the part Hitbox to run a script when it's clicked, and that script being in ServerScriptService (originally, the script started with:

game.Players.PlayerAdded:Connect(function(player: Player)
player.Chatted:Connect(function(message: string, recipient: Player)
if message == "Where am I?" then

and then the function that I want to run)

#

and I'd also like to have a variable that carries over between all scripts/a variable that every script can access and change that this function will access

#

thank you for your help!~

tall coral
#

madam bork lol thats a fun name

solid moon
#

thanks lol

#

by any chance, are you able to provide me with some assistance..?

tall coral
#

yes sorry i'm reading your question

solid moon
#

no worries!

tall coral
#

Okay

#

For clicking on parts you're probably going to want a ClickDetector, which you can connect to and get the clicked event on the server side of things.

#

Your other parts are kind of vague, but it sounds like you also want multiple click detectors in the game that do the same thing

#

I would use collection service Tags to get all the click detectors in the game and then connect to them in your server script and just connect them all to the same function. Then you can have a variable in the main body of that script which all the functions can access in that script.

solid moon
#

mhm! basically, it'll be one huge script with many lines of dialogue. depending on different actions of the player, I'd like to change a value that determines which line of dialogue is used in the dialogue display function that I found

tall coral
#

I recommend separating the long "list" types of scripts from the ones that do functionality. You can make a module script that you can access all the dialogue from.

solid moon
#

okay..! I don't know how to do any of that, sorry- I'm REALLY new. uhmm...wwould you be able to hook me up with a tutorial/guide for newer Studio coders..? ^^"

honest knotBOT
#

studio** You are now Level 1! **studio

tall coral
#

unfortunately I don't really know any that are current because I started a long time ago, and there's a LOT of nuance to this stuff. What I recommend you do is google the words you hear and don't know to try to learn more about them via the official documentation and then try it in-studio for yourself. I'd be happy to answer any questions you have, too.

solid moon
#

okay, thank you!!

tall coral
#

good luck salute

solid moon
#

may I ask, what's wrong with this code..? (just looking at the if then part, since after I added that it stopped working)

local MikaDialogueStep = 1
    if MikaDialogueStep = 1 then
        local dialogue = {
            
            lines = {
                "Oh, thank goodness, you're finally here!"
            }, 
            config = {
                lines = {
                    ["1"] = {
                        Persistent = true,
                        colors = {
                            ["1"] = Color3.fromRGB(255, 255, 255)
                        },
                        sounds = {
                            ["1"] = game.SoundService["DefaultTextSound"],
                        },
                        speeds = {
                            ["1"] = 0.035
                        },
                        effects = {
                            ["1"] = "normal"
                        },
                        fonts = { -- IMPORTANT!!! ONLY 1 FONT CAN BE USED PER LINE OF DIALOGUE OR IT WILL BREAK!!!!!!!!
                            ["1"] = "DefaultFont",
                        }
                    },
                }
            }                    
        }
        game:GetService("ReplicatedStorage")["Customisable Dialogue System Resources"].ActivateDialogueSystem:FireAllClients(dialogue)
end)```
#

this script is inside a part with a click detector, and without the "if then" part it works just fine

tall coral
#

are you sure its detecting the input? can you put a print at the top of the mouseclick function?

#

oh

#

without the if then it works fine

#

oh

#

= is for setting == is the comparator "is equal to"

#

so when making the comparison use the == comparator not the =

solid moon
#

okay, thank you!!

tall coral
solid moon
#

one more thing:

this is my code now, how would I add a check for if the value is 2 that would run after the first if? I assume using elseif, but my tinkering with it trying to get it is not working ^^"

current working code:


script.Parent.ClickDetector.MouseClick:Connect(function(playerWhoClicked: Player) 
    if MikaDialogueStep == 1 then
        local dialogue = {
            lines = {
                "Oh, thank goodness, you're finally here!"
            }, 
            config = {
                lines = {
                    ["1"] = {
                        Persistent = true,
                        colors = {
                            ["1"] = Color3.fromRGB(255, 255, 255)
                        },
                        sounds = {
                            ["1"] = game.SoundService["DefaultTextSound"],
                        },
                        speeds = {
                            ["1"] = 0.035
                        },
                        effects = {
                            ["1"] = "normal"
                        },
                        fonts = { -- IMPORTANT!!! ONLY 1 FONT CAN BE USED PER LINE OF DIALOGUE OR IT WILL BREAK!!!!!!!!
                            ["1"] = "DefaultFont",
                        }
                    },
                }
            }                    
        }
        game:GetService("ReplicatedStorage")["Customisable Dialogue System Resources"].ActivateDialogueSystem:FireAllClients(dialogue)
    end
end)
tall coral
#

yeah elseif would probably be what you're looking for

#

however I might recommend not doing a really long else if chain and instead using a key in a dictionary

solid moon
#

yes! got it working! thank youuu

tall coral
#

uhh like this:

local dialogues = {
  [1] = {
            lines = {
                "Oh, thank goodness, you're finally here!"
            }, 
            config = {
                lines = {
                    ["1"] = {
                        Persistent = true,
                        colors = {
                            ["1"] = Color3.fromRGB(255, 255, 255)
                        },
                        sounds = {
                            ["1"] = game.SoundService["DefaultTextSound"],
                        },
                        speeds = {
                            ["1"] = 0.035
                        },
                        effects = {
                            ["1"] = "normal"
                        },
                        fonts = { -- IMPORTANT!!! ONLY 1 FONT CAN BE USED PER LINE OF DIALOGUE OR IT WILL BREAK!!!!!!!!
                            ["1"] = "DefaultFont",
                        }
                    },
                }
            }                    
        }
}

-- then in your funciton you can do:

local dialogueData = dialogues[MikaDialogueStep]
if not dialogueData then return end

game:GetService("ReplicatedStorage")["Customisable Dialogue System Resources"].ActivateDialogueSystem:FireAllClients(dialogueData)
tall coral
solid moon
# tall coral ping

I sort of understand..? Would you be able to give an example of that and another list item, as well as the function being called to select item 2..? I really appreciate your help!!

tall coral
#

sure 1 sec

#
local dialogues = {
  [1] = {
            lines = {
                "Oh, thank goodness, you're finally here!"
            }, 
            config = {
                lines = {
                    ["1"] = {
                        Persistent = true,
                        colors = {
                            ["1"] = Color3.fromRGB(255, 255, 255)
                        },
                        sounds = {
                            ["1"] = game.SoundService["DefaultTextSound"],
                        },
                        speeds = {
                            ["1"] = 0.035
                        },
                        effects = {
                            ["1"] = "normal"
                        },
                        fonts = { -- IMPORTANT!!! ONLY 1 FONT CAN BE USED PER LINE OF DIALOGUE OR IT WILL BREAK!!!!!!!!
                            ["1"] = "DefaultFont",
                        }
                    },
                }
            }                    
        },
  [2] = {
            lines = {
                "Dialogue 2 electric boogaloo"
            }, 
            config = {
                lines = {
                    ["1"] = {
                        Persistent = true,
                        colors = {
                            ["1"] = Color3.fromRGB(255, 255, 255)
                        },
                        sounds = {
                            ["1"] = game.SoundService["DefaultTextSound"],
                        },
                        speeds = {
                            ["1"] = 0.035
                        },
                        effects = {
                            ["1"] = "normal"
                        },
                        fonts = { -- IMPORTANT!!! ONLY 1 FONT CAN BE USED PER LINE OF DIALOGUE OR IT WILL BREAK!!!!!!!!
                            ["1"] = "DefaultFont",
                        }
                    },
                }
            }                    
        }
}

-- then in your funciton you can do:

local MikaDialogueStep = 1

script.Parent.ClickDetector.MouseClick:Connect(function(playerWhoClicked: Player) 
  local dialogueData = dialogues[MikaDialogueStep] -- look in dialogues for a dialogue table at the current "MikaDialogueStep"
  if not dialogueData then return end -- if there is no dialogueData for the current MikaDialogueStep we end the function early.
  
  MikaDialogueStep += 1 -- increment dialogue step
  game:GetService("ReplicatedStorage")["Customisable Dialogue System Resources"].ActivateDialogueSystem:FireAllClients(dialogueData) -- fire clients with the latest dialogue step.
end)