generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Passing a Function.

Previous Thread :: Next Thread 
leomesa is not online. leomesa
Joined: 11 Apr 2014
Total Posts: 1029
31 Jul 2016 02:20 PM
So I have this local script in StarterGui and I want to be able to pass this function to my server script so that the players mouse target can be destroyed in the server.

Here is my local script:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local MineBlock = game:GetService("ReplicatedStorage").Events:WaitForChild("MineBlock")

function Mine()
local target = mouse.Target

if target == nil then
target = nil
else
target:Destroy()
end
end

mouse.Button1Down:connect(function()
MineBlock:FireServer("Hello")
end)

Here is my script in ServerScriptService:

MineBlock = game:GetService("ReplicatedStorage").Events:WaitForChild("MineBlock")

MineBlock.OnServerEvent:connect(function(plr, Test, Mine)
print(plr.Name, Test)
MineBlock:FireAllClients(plr,'Hello!')
end)
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
31 Jul 2016 02:34 PM
You can't pass functions. You will need to find another way.


Report Abuse
Kofphtastrophe is not online. Kofphtastrophe
Joined: 28 Oct 2015
Total Posts: 154
31 Jul 2016 02:37 PM
I suggest using a ModuleScript, and require the function from that ModuleScript.
For example;

--Put this in the module
local xmodule = {}
function xmodule.fetch()
print("The function in the module was fired")
end
return xmodule


--...And put this in the script that fires the function in the ModuleScript
local ModuleScript = require(workspace:WaitForChild("ExampleModule"))
ModuleScript.fetch() --Fires the function in the module

Report Abuse
leomesa is not online. leomesa
Joined: 11 Apr 2014
Total Posts: 1029
31 Jul 2016 02:40 PM
Wait but can I pass the mouse?
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
31 Jul 2016 02:43 PM
Nope. You can't pass the mouse. You can most likely pass the target though.


Report Abuse
leomesa is not online. leomesa
Joined: 11 Apr 2014
Total Posts: 1029
31 Jul 2016 02:45 PM
Alright I'll try that instead and see what I can do with it.
Report Abuse
leomesa is not online. leomesa
Joined: 11 Apr 2014
Total Posts: 1029
31 Jul 2016 02:52 PM
Ok so what I'm doing seems like it could work but it isn't here is the code:

Local script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local MineBlock = game:GetService("ReplicatedStorage").Events:WaitForChild("MineBlock")

local target = mouse.Target

mouse.Button1Down:connect(function()
MineBlock:FireServer("Hello")
MineBlock:FireServer(target)
end)

ServerScript:

MineBlock = game:GetService("ReplicatedStorage").Events:WaitForChild("MineBlock")

MineBlock.OnServerEvent:connect(function(plr, Test, Block)
print(plr.Name, Test)
local Block = Block
Block:Destory()
MineBlock:FireAllClients(plr,'Hello!')
end)
Report Abuse
Real_Edgy is not online. Real_Edgy
Joined: 23 Oct 2013
Total Posts: 1212
31 Jul 2016 02:54 PM
Your 'target' variable will hold the same thing until the script quits out. It will initialize as nil. You need to get the target _when_ you click.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
31 Jul 2016 02:54 PM
Target doesn't update when mouse.Target updates, because it essentially copies the value.

That isn't true of Instances and Tables, but of most things like strings, it only copies it.


mouse.Button1Down:connect(function()
local target = mouse.Target
MineBlock:FireServer("Hello")
MineBlock:FireServer(target)
end)


Report Abuse
leomesa is not online. leomesa
Joined: 11 Apr 2014
Total Posts: 1029
31 Jul 2016 02:58 PM
Ah alright I understand now and now I fixed it to get target when you click but it comes out with an error in ServerScriptService:

ServerScriptService.GameEventsManager:5: attempt to index local 'Block' (a nil value)
Report Abuse
leomesa is not online. leomesa
Joined: 11 Apr 2014
Total Posts: 1029
31 Jul 2016 03:14 PM
bump
Report Abuse
leomesa is not online. leomesa
Joined: 11 Apr 2014
Total Posts: 1029
31 Jul 2016 03:26 PM
bump
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image