I have a script that plays an animation after a proximity prompt is triggered and the play clicks a button on a gui that is activated by the prompt. The script is supposed to know when the button is pressed by the gui becoming not visible, but this change happens on the client side, so as far as the server is concerned, the gui is still visible. I want to use a RemoteEvent, but I don’t know how to do this because I want many objects being animated all under the same name, and I don’t want them all to play the animation after the prompt is triggered. Here is the script:
local fridgePromt = script.Parent.MiddleShelf.FridgePrompt
local animator = script.Parent.FridgeCore.AnimationController.Animator
local openAnimation = script.Parent.FridgeCore.Animations.DoorOpen
local openAnimTrack = animator:LoadAnimation(openAnimation)
local closeAnimation = script.Parent.FridgeCore.Animations.DoorClose
local closeAnimTrack = animator:LoadAnimation(closeAnimation)
fridgePromt.Triggered:Connect(function(plr)
local PlayerGui = plr:WaitForChild("PlayerGui")
wait(0.3)
local fridgeGui = PlayerGui.FridgeGUI.FridgeGUIFrame
while fridgeGui.Visible == true do
wait(0.3)
end
openAnimTrack:Play()
wait(0.5)
closeAnimTrack:Play()
end)
Sorry for not elaborating. The gui is being opened from the server and closed from the client. The code:
while fridgeGui.Visible == true do
wait(0.3)
end
is waiting for it to be closed. This script is on the server side, and the gui is being closed client side, so the server never sees that the gui is closed. Therefore, this code will wait forever, and nothing will happen, and this is the problem I’m trying to fix.
The proximity prompt is on the server, there is a separate gui on the client, and the player triggers the proximity prompt. When it is triggered, the server opens the separate gui on the client. Then, the player presses a text button, so the client closes the gui on the client.
ohhh, makes much more sense,
let me reiterate what you mean
so basically,
first, player activates proximity prompt
second, a gui pops up for the player client
and third, player presses the button on the gui and there’s a remote event that fires from the client to the server
then, the server waits until it receives the event and finally it plays the animation
i think instead checking under the proximity prompt function, couldn’t you use something on the server side to receive the fired remote event, instead of using a while loop to check for visible property change?
server sided function
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote_event = ReplicatedStorage:WaitForChild("RemoteEvent")
local function playAnimation()
openAnim:Play()
openAnimation.Stopped:Wait()
closeAnim:Play()
end
remote_event.OnServerEvent:Connect(playAnimation)
client sided fire event
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote_event = ReplicatedStorage:WaitForChild("RemoteEvent")
-- Fire the remote event
remote_event:FireServer()
Great code, but there’s one problem. It is a fridge in a restaurant, so I want to replicate it many times, and I don’t want to have to go in a change the code each time, so I don’t know what to do.
Thank you for the suggestion. I have many applications for my game and I am very excited to use it, but I’m not sure how it could help me. Could you please elaborate on your solution?
When you say replicate it multiple times, are you referring to there being multiple fridges, and you would like all of them to have this effect? Also I’d recommend keeping all UI stuff client sided, it helps in the long run. Here is a proposed solution you could, in theory use. If you’re looking to have the animation on the client side, have you thought of TweenService? It might be easier then firing a remote
to activate that fridge.
Keep in mind for this to work, use the Tag Editor plugin as shown above. Click on the plugin, press “Create new tag”, name it “Fridge”, and while you have “Fridge” selected, select all of the fridges in your game.
-- client
local CollectionService = game:GetService("CollectionService")
local Fridges = CollectionService:GetTagged("Fridge")
local fridgeGui = game.Players.LocalPlayer.PlayerGui:WaitForChild("FridgeGUI")
local selectedFridge
for _, fridge in pairs(Fridges) do
fridge.ProximityPrompt.Triggered:Connect(function(plr)
if plr == game.Players.LocalPlayer then
selectedFridge = fridge
fridgeGui.FridgeGUIFrame.Visible = true
end
end)
end
-- at this point, i assume the gui is probably closed by pressing a button. change this to the name of the button.
fridgeGui.FridgeGUIFrame.Close.MouseButton1Click:Connect(function()
fridgeGui.FridgeGUIFrame.Visible = false
game.ReplicatedStorage.RemoteEvent:FireServer(selectedFridge)
end)
-- server
local function AnimateFridge(plr, Fridge)
local animator = Fridge.AnimationController.Animator
local openAnimation = Fridge.Animations.DoorOpen
local closeAnimation = Fridge.Animations.DoorClose
local openAnimTrack = animator:LoadAnimation(openAnimation)
wait(0.5)
local closeAnimTrack = animator:LoadAnimation(closeAnimation)
end
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(AnimateFridge)
and tag all the models you need to, and thats pretty much all the steps to using it
then add a script, if you doing server then preferably in server script service unless its on the client then either add it to starter gui or the start player/character scripts, it kinda depends what you’re going for
local CollectionService = game:GetService("CollectionService")
local Fridges = CollectionService:GetTagged("Fridge")
for _, fridge in pairs(Fridges) do
--code here
end
Do I put the client script in starter player scripts? Also, there are a lot of buttons within the gui that can close the gui, so how would I modify it to allow any button to close it?
I put those scripts in StarterPlayerScripts, because it wasn’t working in StarterGui, but now the game won’t load because it says there is an error loading core scripts: no verified patch could be loaded.