I’ve been making this gun system for a bit of time and I’ve gotten pretty far. My friend gave me the idea to make it stop lagging so far behind when you shoot it, so I decided to make the visual stuff render on the client from a server script.
The issue with it is I send the barrel of the gun (CFrame) to the server, and the server sends it to the client using FireAllClients and it stops at the second line of code. It repeats the following: “Attempt to index CFrame, got nil” and same thing when I tried to use WaitForChild in both the main, and visual scripts.
--Main local script
local tool = script.Parent
local muzzle = tool:WaitForChild("Barrel")
local enabled = false --I already made a function that checks if the tool is equipped, so ignore this.
mouse.Button1Down:Connect(function()
if enabled == true then
local raycast = workspace:Raycast(startingpos, (mouse.Hit.p - startingpos).Unit *300,rc)
if raycast then
print("Shot")
game.ReplicatedStorage.GunFE.Visual:FireServer(mouse.Hit.p, muzzle)
local result = raycast.Instance.Parent or raycast.Instance.Parent.Parent or raycast.Instance.Parent.Parent.Parent
if result:FindFirstChild("Humanoid") ~= nil then
local newplr = game:GetService("Players"):GetPlayerFromCharacter(result)
if result.Team ~= plr.Team then
game.ReplicatedStorage.GunFE.Shot:FireServer(result:FindFirstChild("Humanoid"),damage)
end
end
end
end
end)
--Server Script
game.ReplicatedStorage.GunFE.Visual.OnServerEvent:Connect(function(plr, mouse, barrel)
game.ReplicatedStorage.GunFE.Client:FireAllClients(mouse,barrel)
end)
--Visual local script
game.ReplicatedStorage.GunFE.Client.OnClientEvent:Connect(function(plr, mouse, barrel)
local newpos = barrel.CFrame.p
print(newpos)
local bip = (mouse - newpos).Unit * 300
local dir = newpos + bip/2
print(dir)
local bullet = Instance.new("Part")
bullet.Parent = workspace
bullet.Color = Color3.fromRGB(255,255,0)
bullet.Material = Enum.Material.Neon
bullet.Anchored = true
bullet.CanCollide = false
bullet.CFrame = CFrame.new(dir, newpos)
bullet.Size = Vector3.new(.3,.3,dir.Magnitude)
game:GetService("Debris"):AddItem(bullet,0.05)
end)
I’ve searched through each script and made various edits, but it is apparent that I am missing something. Not sure why it is returning a nil value each time.