hello , whenever i have 3 scripts for a gun and whenever the gun is shot it fires a remote event and fires to all clients to a local script inside starter player scripts to show the bullet for all players.
it works when you are by yourself howerver when a new player joins it duplicates and keeps on duplicating whenever more people are joining. any help is appreciated
local script for gun
local UIS = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local tool = script.Parent
local barrel = tool.Barrel
local Animations = {
"rbxassetid://10776678048", -- idle
"rbxassetid://" -- shoot
}
local shooting = false
local function playAnimation(id, name, loop)
local character = player.Character or player.CharacterAdded:Wait()
local Animation = Instance.new("Animation")
Animation.AnimationId = id
Animation.Name = "gun_"..name
local AnimTrack = character.Humanoid:LoadAnimation(Animation)
AnimTrack:Play(0, 1, 1)
if not loop then
task.delay(AnimTrack.Length, function()
AnimTrack:Stop()
AnimTrack:Destroy()
end)
end
end
local function Equip()
playAnimation(Animations[1], "idle", true)
end
local function unEquip()
local character = player.Character or player.CharacterAdded:Wait()
for _, track in pairs(character.Humanoid:GetPlayingAnimationTracks()) do
if track.Name:match("gun") then
track:Stop()
track:Destroy()
end
end
end
local fireEvent = replicatedStorage:WaitForChild("Events").fireEvent
local bulletEvent = replicatedStorage:WaitForChild("Events").bulletEvent
local damage = script.Parent:GetAttribute("Damage")
local range = 400
local resultParams = RaycastParams.new()
resultParams.FilterDescendantsInstances = {player.Character, tool, workspace.Bullets}
resultParams.FilterType = Enum.RaycastFilterType.Blacklist
local function Shoot(input, gameProc)
if gameProc then return end
if tool.parent == character then
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local mousePos = mouse.Hit.Position
local barrelPos = barrel.Position
local direction = (mousePos - barrelPos).Unit * range
local ray = workspace:Raycast(barrelPos, direction, resultParams)
if ray then
local character = ray.Instance.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid and humanoid ~= player.Character.Humanoid then
print("hit")
fireEvent:FireServer(humanoid, damage)
else
local cframePos = CFrame.new(ray.Position, ray.Position + ray.Normal)
--local part = replicatedStorage:WaitForChild("vfx").bullet_impact.concrete:Clone()
--part.CFrame = cframePos
--part.Attachment.Smoke:Emit(35)
--part.Parent = workspace.Bullets
--debris:AddItem(part, 5)
bulletEvent:FireServer(cframePos)
end
end
end
end
end
UIS.InputBegan:Connect(Shoot)
tool.Equipped:Connect(Equip)
tool.Unequipped:Connect(unEquip)
gun server script
local RS = game:GetService("ReplicatedStorage")
local fireEvent = RS:WaitForChild("Events").fireEvent
local bulletEvent = RS:WaitForChild("Events").bulletEvent
local function damagePlayer(player, humanoid, damage)
humanoid:TakeDamage(damage)
end
local function bulletHoleToPlayers(player, cframePos)
bulletEvent:FireAllClients(cframePos)
--print("server Recieved")
--for _,plr in pairs(game.Players:GetChildren()) do
-- if plr ~= player then
-- print("Sent")
-- bulletEvent:FireClient(plr, cframePos)
-- end
--end
end
bulletEvent.OnServerEvent:Connect(bulletHoleToPlayers)
fireEvent.OnServerEvent:Connect(damagePlayer)
second local script in starter player
local replicatedStorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
local bulletEvent = replicatedStorage:WaitForChild("Events").bulletEvent
local function BulletRender(cframePos)
print("client Recieved")
local part = replicatedStorage:WaitForChild("vfx").bullet_impact.concrete:Clone()
part.CFrame = cframePos
part.Attachment.Smoke:Emit(35)
part.Parent = workspace.Bullets
debris:AddItem(part, 5)
end
bulletEvent.OnClientEvent:Connect(BulletRender)