What's wrong with these scripts

Why are these scripts not working. I am trying to get it to where the player presses a TextButton, it fires a remote event that changes the text of a surfacegui textlabel under a part to a random joke. It changes for all players. It’s not working.
I have no idea where to start.

Script 1 - StarterGui
image

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("ChangeJokeText")

local textButton = script.Parent

local function onButtonClicked()
	remoteEvent:FireServer()
end

textButton.MouseButton1Click:Connect(onButtonClicked)

Script 2- - ServerScriptService

-- ServerScriptService
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

local remoteEvent = ServerScriptService:WaitForChild("ChangeJokeText")

local jokes = {
	"Why don't some couples go to the gym? Because some relationships don't work out!",
	"I told my wife she was drawing her eyebrows too high. She seemed surprised.",
	-- Add more jokes here
}

remoteEvent.OnServerEvent:Connect(function(player)
	local randomJoke = jokes[math.random(1, #jokes)]
	local jokeTextLabel = game.Workspace:FindFirstChild("JokeTextLabel")
	if jokeTextLabel and jokeTextLabel:IsA("TextLabel") then
		jokeTextLabel.Text = randomJoke
	end
end)

2 Likes

local remoteEvent = ServerScriptService:WaitForChild(“ChangeJokeText”)

The event is still in replicated storage for the server because it’s in storage thats replicated across the client and server, you should define the event the same way you defined it on the client.

image

I meant you need to define the event on the server the same way you did on the client not the other way round.

My point was you’re looking for the event in serverscriptservice which isn’t where remote events should be, they should be placed in replicated storage as replicated storage is replicated across the client and server which is how the events function.

Thank’s a lot! You really helped.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.