Gui button = Server Message

I made this dice system that give u a random number, i would need to be able to send that random number in the server chat (so anyone can read it) directly with the same button that generated it.

The issue is that i dont really understand how to make it work, if u look at the code u can see i writed “num” as the message (thats the “output” of the dice roll) even if im sure its not working like that it wont even say num in chat

I tryed to look online for solution but i was not able to find anything usefull

I came up with a script but its not working:

script.Parent.Touched:Connect(function()
local message = “num”
game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(message)
end)

(also tryed “MouseButton1Click” instead of “Touched”

1 Like

You’ve kind of got the right idea. If I’ve understood you right, every time the player rolls a dice you want it to display in everyone’s chat what they rolled?

You’re gonna want to place a BindableEvent in ReplicatedStorage. Let’s call it “RollEvent”.

--LocalScript
local rStorage = game:GetService("ReplicatedStorage")
local event = rStorage:WaitForChild("RollEvent")

local message = "num"
event:Fire(message)

Then, under StarterPlayerScripts, you want a LocalScript. This will receive the event and display the message:

local tcs = game:GetService("TextChatService")
local rStorage = game:GetService("ReplicatedStorage")
local event = rStorage:WaitForChild("RollEvent")

local function processEvent(message)
    tcs.TextChannels.RBXGeneral:DisplaySystemMessage("<font color=\"rgb(255, 0, 0)\">Rolled: "..message.."</font>")
end

event.Event:Connect(processEvent)

ty for the quick reply, i would like to know where exactly i have to put the first LocalScript & also if i have to keep enabled my script or disable it and use only your. (extra: if possible it would be good to get the name of the player that rolled, i might be able to modify the script myself to add that but if u could help me out also with that i would be really thankfull)

about the image right here: “LF” is the roll script and the LocalScript is the script i writed in the first message (mine)
image

this is what happen if i put your first script into the LocalScript inside the textbutton (send the msg when i join)
image

the roll script im using:

You could combine both scripts. As for getting the name, just use the LocalPlayer object because it’s a LocalScript.

local player = game:GetService("Players").LocalPlayer
local event = game:GetService("ReplicatedStorage"):WaitForChild("RollEvent")

local function processClick()
    local num = math.random(1, 20)
    script.Parent.Frame.output.Text = num
    event:Fire(player.Name, num)
end

script.Parent.Frame.TextButton.MouseButton1Click:Connect(processClick)

Then, modify the receiver slightly:

local function processEvent(playerName, num)
    tcs.TextChannels.RBXGeneral:DisplaySystemMessage("<font color=\"rgb(255, 0, 0)\">@"..playerName.." rolled: "..num.."</font>")
end

event.Event:Connect(processEvent)

If you want to use the player’s DisplayName, when firing the event, just use player.DisplayName instead of player.Name.

honestly i think im messing it up (completly) could you explane the exact location where i have to put the scripts (honestly its some months im not touching studio and i just started trying to do this system yesterday)

image

second image to show (the textbutton i tryed to edit at the start its inside the frame)
image

Sorry, I just spotted an error…
Make the BindableEvent into a RemoteEvent with the same name, and in the same place.

Place this as a child to the GUI:

--LocalScript
local player = game:GetService("Players").LocalPlayer
local event = game:GetService("ReplicatedStorage"):WaitForChild("RollEvent")

local function processClick()
    local num = math.random(1, 20)
    script.Parent.Frame.output.Text = num
    event:FireServer(num)
end

script.Parent.TextButton.MouseButton1Click:Connect(processClick)

and place this under StarterPlayerScripts:

--LocalScript
local function processEvent(playerName, num)
    tcs.TextChannels.RBXGeneral:DisplaySystemMessage("<font color=\"rgb(255, 0, 0)\">@"..playerName.." rolled: "..num.."</font>")
end

event.OnClientEvent:Connect(processEvent)

Then, have a Script in ServerScriptService:

--Script
local event = game:GetService("ReplicatedStorage").RollEvent

local function processRoll(player, num)
    if typeof(num) ~= "number" then return end --anticheat
    event:FireAllClients(player.Name, num)
end

event.OnServerEvent:Connect(processRoll)
1 Like

still not working (also tryed to put the script for the GUI first into “LF” then into the textbutton “LocalScript”

also there is the part to close the roll thing (wich i guess is not breaking anything) so its simply not working

btw just to show it, here the GUI (there is a 65 bc that was the d100 not the d20)

ok, are there any errors in the output?

for other things in game (about 5 mil+ errors) but let me check

“Lemme put the dice thing in another place” there are too many errors

1 Like

just noticed this in the starter player script

imma send all the scripts so u can review everything

serverScriptService script

LF (its a LocalScript)

then the player one is the one i sent before and the textbutton one is empty

Sorry, I meant keep the variables from the script I sent before…
Here is the whole script (displaying the message):

local tcs = game:GetService("TextChatService")
local event = game:GetService("ReplicatedStorage"):WaitForChild("RollEvent")

local function processEvent(playerName, num)
    tcs.TextChannels.RBXGeneral:DisplaySystemMessage("<font color=\"rgb(255, 0, 0)\">@"..playerName.." rolled: "..num.."</font>")
end

event.OnClientEvent:Connect(processEvent)

By the way, you don’t need any of the comments in the scripts. The comments are anything in grey, after a -- :slight_smile:
Hope this helps.

ok so it kinda of worked but not completly, the dice basically autorolled when i joined and i cant roll it anymore

also imma send a error i got (nothing to do with the dice thing) its just a error i had for like 1 year + (wich wont let me move my camera in studio and its like stuck on the spawn location when i test play)
image

It’s auto rolling because you said you made it do that before. I think the reason It’s lot letting you roll again is because you’re making the frame invisible and never aetting it to visible again - then again, I’m quite tired, so I might be wrong…

the frame is invisible, then this button make it visible
image
and then the only way to close it is to click the close button that u can see here, so i think its not like u said
image
just to clarify with my base scripts was working like this (and also now) but the only way to spin was to click the actual “Roll D 20” button

maybe I’m a dum dum, but I think text only shows strings and not number values, so I suggests converting the number into a string by using num:ToString()

Nah nvm, just saw this post, so you got a problem repeating the script again?

if i completly understood what u said i actually dont need to do it, bc for now the dice rolled (automatically only once) and told the actual number that popped up, so its good, but the only problem is that i cant spin anymore

I got an hour before I go to bed, Imma help you get this through, should be easy enough.

i mean ye, ofc it should not auto on playerjoin (but i dont care if it does it) i just need it to work when i click the next time (for like infinite times)