I’m trying to create a projectile but whenever I try to fire to the client to create effects on the client, I can’t send the projectile/instance to the clients.
instance as shown in the client script prints nil.
Server:
--Variables are above
local Basic = {}
Basic.__index = Basic
function Basic.new(player,pos,currentPoop)
local self = setmetatable({},Basic)
self.Player = player
self.Character = player.Character
self.Humanoid = self.Character.Humanoid
self.Root = self.Character.HumanoidRootPart
self.Part = Instance.new("Part")
self.RayDir = pos
self.CharactersTouched = {}
self.CurrentPoop = currentPoop
return self
end
function Basic:Hitbox()
self.Part.Touched:Connect(function(hit)
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if character and humanoid and not self.CharactersTouched[character] and character ~= self.Character then
self.CharactersTouched[character] = character
humanoid:TakeDamage(DAMAGE)
end
end)
end
function Basic:Destroy()
task.delay(DESTROY_TIME,self.Part.Destroy,self.Part)
end
function Basic:Create()
self.Part.Size = Vector3.new(2,2,2)
self.Part.CanCollide = false
self.Part.Anchored = true
self.Part.Massless = true
self.Part.Position = self.Root.Position + self.Root.CFrame.LookVector * 2
self.Part.Parent = workspace
end
function Basic:Velocity()
self.Part.AssemblyLinearVelocity = self.RayDir * 1000
end
function Basic:Init()
self:Create()
self:Hitbox()
self:Velocity()
self:Destroy()
Remotes.ToClient:FireAllClients("FX",self.CurrentPoop,self.Part)
end
return Basic
Client:
Remotes.ToClient.OnClientEvent:Connect(function(signal,cp,instance)
if signal == "FX" then
print(cp,instance)
Modules[cp].new(instance):Init()
end
end)
Hello, I believe I know some approaches that will help you with this.
When you want to create an instance on the client side based on server side information, rather than passing the instance itself, pass the essential information needed to create the instance on the cllint side. This information can include things like the position, size, properties, and other necessary stuff.
On the client side, receive this information through RemoteEvents or RemoteFunctions, and use it to create the instance locally on the client.
Send the information about the instance from the server to the client. This information can include the position, size, properties, and any other data.
On the client side, receive this information and use it to create a representation of the instance.
An example of doing this is:
Server:
function Basic:Init()
self:Create()
self:Hitbox()
self:Velocity()
self:Destroy()
Remotes.ToClient:FireAllClients("FX", self.CurrentPoop, self.Part.Position, self.Part.Size)
end
client:
Remotes.ToClient.OnClientEvent:Connect(function(signal, cp, position, size)
if signal == "FX" then
local newVisual = Instance.new("Part")
newVisual.Size = size
newVisual.Position = position
newVisual.Color = Color3.fromRGB(255, 0, 0) -- Example color for visualization
newVisual.Parent = workspace
end
end)