Is it not possible to clone a model via a Localscript, and the model contains a local script? Does the local script not fire? Like is anything in the workspace only server sided scripts can be run?
Basically, I’m making a Zombie game. And to really make the encounters and what not unique I wanted to make zombies locally for x player in x region. But is this not possible?
Ah, what I was doing is I had a local script inside a model. And via a GUI / Mouseclick I parented the model to workspace with the local script inside. In this instance it doesn’t work?
you dont need to. its optional, im saying put the modulescript inside your instance and require it in playerscripts, but make sure to put instance arg.
You can definitely create an NPC using just module scripts, and you can use the method I was talking about too.
e.g. same local script:
local npc = -- your npc
require(npc.RunScript)
and the npc’s RunScript (which is still a module script):
local Players = game:GetService("Players")
local NPC = script.Parent
local humanoid = NPC:WaitForChild("Humanoid")
local function getNearestTarget()
-- I don't feel like writing that,
-- but it returns the nearest player
end
-- this is wrapped in a coroutine so that it doesn't yield forever.
local followLoop = task.defer(function()
while true do
local target = getNearestTarget()
local targetRoot = target.Character.HumanoidRootPart
humanoid:MoveTo(targetRoot.Position)
task.wait(0.5)
end
end)
humanoid.Died:Connect(function()
task.cancel(followLoop)
-- tell the humanoid to stop moving
humanoid:Move(Vector3.new(0, 0, 0))
end)
return nil
The first time you require the RunScript module is the moment all this code gets executed.
There are other ways to structure the module if you don’t like doing it like this, like putting all this code in some functions in a table and returning the table so that the local script can run all the logic.
#2) If I’m to make some sort of detection for lets say bullets. Like I’ve got a gun that creates bullets locally. Would a simple part.touched event run if the bullet and the part are both local parts?
task.defer runs the inner function argument in the very near future, like 0.001 seconds in the future, and lets that code run at the same time as other code. task.spawn does the same thing but runs the inner function first until it yields (via task.wait, event:Wait(), or an Async / WaitForChild method).
That would work if the bullet is fired slowly enough, but I wouldn’t recommend it. I would use raycasting. You can either write the raycasts yourself or use a module like FastCast.
So is task.defer essentially what you’d use when you don’t want to have to call the function? And that’s also cool that you can cancel (in the death event)
Would you be able to give me an example of a raycast from x part → player mouse.hit?
task.defer / task.spawn is what you’d use when you want to run a function at the same time as everything else. I still want to call the function, I just don’t want it to only run the stuff in the function. I want it to do the stuff after it too.
Here’s an Intro to Raycasting tutorial from the Dev Hub, It should answer most of your questions. You don’t really have to pay attention to the code snippets, reading the normal text should be enough.
And your example I guess:
local Workspace = game:GetService("Worksapce")
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.FilterDescendantsInstances = { --[[ player's character ]] }
local function raycast(xPart: BasePart, mouseHit: CFrame): RaycastResult
local origin = xPart.Position
local direction = mouseHit.Position - xPart.Position
local result = Workspace:Raycast(origin, direction, rayParams)
return result
end