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”
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)
this is what happen if i put your first script into the LocalScript inside the textbutton (send the msg when i join)
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)
second image to show (the textbutton i tryed to edit at the start its inside the frame)
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)
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 --
Hope this helps.
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)
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…
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()
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 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)