-
What do you want to achieve? Keep it simple and clear!
Hello! I’m trying to make a bomb tool that can throw a bomb when the user clicks on their left mouse button. In the code snippets below, I am trying to tell the server (from the client) to create the bomb part for me, then have the server create the bomb and send back a reference to the created part to the client. -
What is the issue? Include screenshots / videos if possible!
The bomb is successfully being created on the server side and it is replicated to the client (because the bomb shows up in the client’s workspace), but when the server fires to the client using fireClient, the bomb instance that’s passed in is nil when I check its value on the client.
The first print statement tells me that the bomb was created on the server side, but then on the client, the bomb returned by the createBomb function has its collision group set. However, as you can see, the command is failing with the “attempt to index nil” error.
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried setting up the event connection before and after the FireServer method, but I still receive nil from the server.
createBombRequest:FireServer(props)
local bomb = createBombRequest.OnClientEvent:Wait()
-- bomb is nil
return bomb
local bomb
createBombRequest.OnClientEvent:Connect(function(serverBombPart)
bomb = serverBombPart
end)
createBombRequest:FireServer(props)
return bomb
-- bomb is still nil
Here is my current code.
Client Script:
local physProperties = PhysicalProperties.new(DENSITY, FRICTION, ELASTICITY, FRICTION_WEIGHT, ELASTICITY_WEIGHT)
function createBomb()
-- we need to set the properties, then fire a remote event with the properties, then let the server handle the object creation, then get the object that the server created.
local props = {}
props.Position = Bomb.Position
props.Size = Vector3.new(4,4,4)
props.BrickColor = BrickColor.new(21)
props.Shape = 0
props.BottomSurface = 0
props.TopSurface = 0
props.Reflectance = .5
props.Name = "TimeBomb"
props.Locked = true
props.CustomPhysicalProperties = physProperties
props.Parent = game.Workspace
props.Transparency = 0
createBombRequest:FireServer(props)
local bomb = createBombRequest.OnClientEvent:Wait()
return bomb
end
Server Script:
local replicatedStorage = game:GetService("ReplicatedStorage")
local createBombRequest = replicatedStorage.CreateBombRequest
createBombRequest.OnServerEvent:Connect(function(plr, props)
local bomb = Instance.new("Part")
for i,v in pairs(props) do
bomb[i] = v
end
--bomb.Parent = replicatedStorage
bomb:SetNetworkOwner(plr)
-- created bomb
print("Created bomb", bomb, "for player", plr, "Firing event to client")
createBombRequest:FireClient(plr, bomb)
end)