In the game I am currently making, I have a fridge where one can get different types of food on their plate using a proximity prompt. But if the user does not have a plate in hand, I have the script fire a remote event to allow an error to pop up explaining they need a plate. How can I make it so the remote event can’t be called by that player until the function in the code listed below is finished. Thanks!
game.ReplicatedStorage.Events.Alert.OnClientEvent:Connect(function(err)
local player = game.Players.LocalPlayer
local Alert = player.PlayerGui.Alert
print(player.Name .. " has gotten error: " .. err)
Alert.Frame.Frame.TextLabel.Text = err
Alert.Frame:TweenPosition(UDim2.new(0.02, 0,0.87, 0), "Out", "Linear", 0.2, true)
wait(2)
Alert.Frame:TweenPosition(UDim2.new(-0.3, 0,0.87, 0), "Out", "Linear", 0.2, true)
end)
You can’t block a remote event, but you can have your code either ignore an incoming event or know not to send an event.
For example, you can ignore the event with a debounce:
local debounce = false
game.ReplicatedStorage.Events.Alert.OnClientEvent:Connect(function(err)
if debounce then return end -- another error is being displayed, ignore this error
debounce = true
local player = game.Players.LocalPlayer
local Alert = player.PlayerGui.Alert
print(player.Name .. " has gotten error: " .. err)
Alert.Frame.Frame.TextLabel.Text = err
Alert.Frame:TweenPosition(UDim2.new(0.02, 0,0.87, 0), "Out", "Linear", 0.2, true)
wait(2)
Alert.Frame:TweenPosition(UDim2.new(-0.3, 0,0.87, 0), "Out", "Linear", 0.2, true)
debounce = false
end)
Or, keep track of this debounce on the server:
local AlertCooldown = {}
-- wherever event is being fired
if not AlertCooldown[player] then -- if player is not on alert cooldown
game.ReplicatedStorage.Events.Alert:FireClient(player, "Oh no!")
AlertCooldown[player] = true -- set flag that this player is on cooldown
task.delay(2, function() -- wait 2 seconds without yielding code
AlertCooldown[player] = nil -- remove the flag
end)
end