Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I need help on server - client communication using remotefunction?

Script

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

local clickDetector = script.Parent.ClickDetector

local shopOpenRequest = Instance.new("RemoteFunction")
shopOpenRequest.Name = "ShopOpenRequest"
shopOpenRequest.Parent = ReplicatedStorage

local function clicked(player)
	shopOpenRequest:InvokeClient(player)
end

clickDetector.MouseClick:Connect(clicked)

Local Script

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

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local shopOpenRequest = ReplicatedStorage:WaitForChild("ShopOpenRequest")

local shop = script.Parent.Parent
shop.Enabled = false

local function onShopOpenRequested()
	shop.Enabled = true
end

shopOpenRequest.OnClientInvoke = onShopOpenRequested()

This script would run immediately after you join server and never work again. I need solution on how to make this script run only when the clickDetector is activated and run more than once.

1 answer

This happens because of shop.Enabled = true. Basically, whenever the player clicks on the button, shop.Enabled will be set to true, and not get back to false.

Here's a better version of the OnShopOpenRequested() :

local function onShopOpenRequested()
	shop.Enabled = not shop.Enabled -- This is a toggle basically
end

shopOpenRequest.OnClientInvoke = onShopOpenRequested()

1 comment

0

Thanks but It doesn't work

This discussion is preserved as it was. New answers, comments, and votes are closed.