I’ve been trying all day to get this right and keep failing.
I have a GUI with a button. I only want the button to run code (if conditions are met) I also don’t want any hackers manipulating the local scripts.
So at first, I tried doing a client → server event and in the server script I checked if the conditions were met. And then I needed to do GUI changes. So inside that ‘OnServerEvent’
I then sent a server → client event back to the original local script and here inside the ‘OnClientEvent’ function I then want to start doing the GUI changes. But when I try to do the server → client it doesn’t seem to be working. Is it because you can’t send server → client inside of a function that’s being called on ‘OnServerEvent’ ?
Is there a better way to do this? This is my code:
Local Script: Client → Server
local frame = script.Parent
frame:WaitForChild("Accept").MouseButton1Down:Connect(function(Accept)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvents").RE2
remoteEvent:FireServer("MissionName")
end)
Server script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvents").RE3
game.ReplicatedStorage:WaitForChild("RemoteEvents").RE2.OnServerEvent:Connect(function(player,missionName)
local MissionValues = player.PlayerGui:WaitForChild("MissionValues")
if (MissionValues.CurrentMissions.Value < 3) then
remoteEvent:FireClient(player, true)
end
end)
So now inside this function I then fire back from the Server → Client
Local script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvents").RE3
local function missionAccepted(player, status)
if status == true then
print("Can do mission")
end
end
remoteEvent.OnClientEvent:Connect(missionAccepted)
This function doesn’t run ^ Any ideas? What’s wrong, or how to do it better?
Edit: I know what’s the issue I just realized I’m removing this local script once the client → server runs. So when the server tries to go back to the client, the script is gone. Thanks.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvents").RE3
remoteEvent:FireClient(player, true)
local script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvents").RE3
local function missionAccepted(player, status)
print("Test")
if status == true then
print("Can do mission")
frame.Parent:Remove()
end
end
remoteEvent.OnClientEvent:Connect(missionAccepted)
So “test” is being outputted, but status doesn’t seem to be equal to true, any idea why that is? Is my argument set up wrong?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvents").RE3
remoteEvent.OnClientEvent:Connect(function(player,status)
print("Test")
if status == true then
print("Can do mission")
frame.Parent:Remove()
end
end)
Just a small change I wanna see if it makes a difference.
I found the issue the receiving end (server → client) does not need player in it’s first parameter. So in this case because I put (player, status) it was assuming player was the boolean.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvents").RE3
local function missionAccepted(player, status)
print("Test", status)
if status == true then
print("Can do mission")
frame.Parent:Remove()
end
end
remoteEvent.OnClientEvent:Connect(missionAccepted)