Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local module = {}
- local actions = {}
- local request = {}
- local actionCooldowns = {}
- local requestCooldowns = {}
- local actionCons = {}
- local function getFunction(player)
- if(not player)then warn("please provide a player to fire the client") end
- return player:WaitForChild("SlimFunction")
- end
- local function getEvent(player)
- if(not player)then warn("please provide a player to fire the client") end
- return player:WaitForChild("SlimEvent")
- end
- --Start listening for action and request IDs from a specified client
- function module:StartListening(player)
- if(not player)then return end
- --give the player a remote event for actions
- local remoteEvent = Instance.new("RemoteEvent")
- local onServerEvent = remoteEvent.OnServerEvent
- remoteEvent.Name = "SlimEvent"
- remoteEvent.Parent = player
- --give the player a remote function for request
- local remoteFunction = Instance.new("RemoteFunction")
- remoteFunction.Name = "SlimFunction"
- remoteFunction.Parent = player
- --handle player actions
- actionCons[player.Name] = onServerEvent:Connect(function(player, id, data)
- --if action ID doesn't even exist then return
- if(not actions[id]) then return end
- --if a cooldown exists for this players action then return
- if(actionCooldowns[player.Name]) then
- local prevAction = actionCooldowns[player.Name].PreviousActions[id]
- if(prevAction and os.clock() < prevAction)then
- return
- end
- else
- actionCooldowns[player.Name] = {PreviousActions = {}}
- end
- --if there is no cooldown currently, then store its cooldown value and run the players action
- actionCooldowns[player.Name].PreviousActions[id] = os.clock() + actions[id].Cooldown
- actions[id].Run(player, data)
- end)
- --handle player made request
- remoteFunction.OnServerInvoke = function(player,id,data)
- --if action ID doesn't even exist then return
- if(not request[id]) then return end
- --if a cooldown exists for this players action then return
- if(requestCooldowns[player.Name]) then
- local prevRequest = requestCooldowns[player.Name].PreviousRequest[id]
- if(prevRequest and os.clock() < prevRequest)then
- return
- end
- else
- requestCooldowns[player.Name] = {PreviousRequest = {}}
- end
- --if there is no cooldown currently, then store its cooldown value and handle the players request
- requestCooldowns[player.Name].PreviousRequest[id] = os.clock() + request[id].Cooldown
- return request[id]:Run(player,data)
- end
- end
- --Stop handling client request / actions
- function module:StopListening(player)
- if(not player)then return end
- --dispose of remote event connection/cooldowns
- if(actionCons[player.Name])then
- actionCons[player.Name]:Disconnect()
- actionCons[player.Name]=nil
- end
- if(actionCooldowns[player.Name])then
- actionCooldowns[player.Name]=nil
- end
- --dispose of remote function cooldowns
- if(requestCooldowns[player.Name])then
- requestCooldowns[player.Name]=nil
- end
- end
- --Fire a client to do an action
- function module:Fire(player,id,data)
- local remote = getEvent(player)
- if(remote)then
- remote:FireClient(player,id,data)
- end
- end
- --Fire all clients to do an action
- function module:FireAll(id,data)
- for _, player in pairs(game.Players:GetPlayers())do
- local remote = getEvent(player)
- if(remote)then
- remote:FireClient(player,id,data)
- end
- end
- end
- --Handle an event
- function module:HandleEvent(id,cooldown,f)
- actions[id] = {Cooldown = cooldown, PreviousEvents = {}, Run = f}
- end
- --Request something from the client
- function module:Request(player, id, data)
- local remoteFunction = getFunction(player)
- if(remoteFunction)then
- return remoteFunction:InvokeClient(player, id, data)
- end
- end
- --Handle a request from the client
- function module:HandleRequest(id, cooldown, f)
- local newRequest = {Cooldown = cooldown}
- function newRequest:Run(player,data)
- return f(player,data)
- end
- request[id]=newRequest
- end
- return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement