Tools from two different players are Linked

  1. What do you want to achieve?
    Multiple players with guns that work independently of eachother.

  2. What is the issue? Include screenshots / videos if possible!
    I have guns in my game, one in starterpack and a few in a shop. Just talking about the starterpack weapon; when one player joins everything works as intended, but when another joins every function seems to be linked such as reloading at an ammotable, it not only reloads for the person that touches it but also everybody else.

Video Demonstration: BugWithTools - YouTube

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked for solutions but none seem to have the same problem, In the serverside script for the weapon I reference localplayer by using PlayerAdded, which may overwrite when a new player joins(?)

I have also tried putting the serverside script for the tool (Which handles the raycasts and damage/headshot sound) inside the tool instead of serverscriptservice but that also does not work.

--------LocalScript inside of the weapon (“Steyr M”) in StarterPack------------

local origin = script.Parent:WaitForChild("Origin")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local tool = player.Backpack:WaitForChild("Steyr M")

local Steyrshoot = game.ReplicatedStorage.Steyrfire
local BulletFolder = game.Workspace.Bullets

local UIS = game:GetService('UserInputService') -- for checking keys
local ui = script.Parent:WaitForChild("ScreenGui") --get screengui

local debounce = false
local cooldown = 0.15

local doing = false

local animShoot = Instance.new("Animation")
animShoot.AnimationId = "http://www.roblox.com/Asset?ID=11723393460"
local track1

ammotable = game.Workspace.Map.MainMap.Ammotable.AmmotableMain

local steyrammo = 10 -- ammo in a mag
local steyrmags = 2 -- magazines

tool.Equipped:Connect(function()
	mouse.Icon = "rbxassetid://11703272985"
	ScreenGui = ui:Clone()
	ScreenGui.Parent = game.Players.LocalPlayer.PlayerGui
	ScreenGui.Body.Ammo.Text = (steyrammo)
	ScreenGui.Body.Mags.Text = (steyrmags)
	lookat = true
	while lookat  do
		local Player = game.Players.LocalPlayer
		local Mouse = Player:GetMouse()
		local Character = Player.Character
		local Root = Character.HumanoidRootPart

		local RootPos, MousePos = Root.Position, Mouse.Hit.Position
		Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
		task.wait()
	end
end)

tool.Unequipped:Connect(function()
	mouse.Icon = [[ ]]
	ScreenGui:Destroy()
end)

local tabledebounce = false

local function TouchTable()
	if tabledebounce == false then
		if player.Character:FindFirstChild("Steyr M") ~= nil then
			tabledebounce = true
			steyrmags = 2
			steyrammo = 10
			ScreenGui.Body.Ammo.Text = (steyrammo)
			ScreenGui.Body.Mags.Text = (steyrmags)
			tool.Origin.TakeAmmo:Play()
			task.wait(3)
			tabledebounce = false
		end
	end
end

ammotable = game.Workspace.Map.MainMap.Ammotable.AmmotableMain
ammotable.Touched:Connect(TouchTable)
ammotable2 = game.Workspace.Map.MainMap.Ammotable2.AmmotableMain
ammotable2.Touched:Connect(TouchTable)
ammotable3 = game.Workspace.Map.MainMap.Ammotable3.AmmotableMain
ammotable3.Touched:Connect(TouchTable)

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		if doing == false then
			if steyrmags > 0 then
				if debounce == false then
					if steyrammo < 10 then
						if player.Character:FindFirstChild("Steyr M") ~= nil then
							debounce = true
							script.Parent.Origin.ak47reload:Play()
							steyrmags = steyrmags - 1
							local animReload = Instance.new("Animation")
							animReload.AnimationId = "http://www.roblox.com/Asset?ID=11723458951" 
							local track = script.Parent.Parent.Humanoid:LoadAnimation(animReload)
							track.Priority = Enum.AnimationPriority.Action4
							track.Looped = false
							track:AdjustSpeed(1)
							track:Play()
							task.wait(1.2)
							steyrammo = 10
							ScreenGui.Body.Ammo.Text = (steyrammo)
							ScreenGui.Body.Mags.Text = (steyrmags)
							debounce = false
						end
					end
				end
			end
		end	
	end
end)

tool.Activated:Connect(function()
if steyrammo > 0 then
	
	if debounce == false then
	debounce = true 
		
		steyrammo = steyrammo - 1
		ScreenGui.Body.Ammo.Text = (steyrammo)
		
	local Player = game:GetService('Players').LocalPlayer
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild('Humanoid')
	
	local mousePosition = mouse.Hit.p
	local originPosition = origin.Position
	
	
	track1 = script.Parent.Parent.Humanoid:LoadAnimation(animShoot)
	track1.Priority = Enum.AnimationPriority.Action4
	track1.Looped = false
	track1:Play()

	Steyrshoot:FireServer(mousePosition, originPosition)
	script.Parent.Origin.M249fire:Play()
	wait(cooldown)
	
	local Player = game.Players.LocalPlayer
	local Character = Player.Character
	local Root = Character.HumanoidRootPart

	local Mouse = Player:GetMouse()
	local RunService = game:GetService("RunService")
	
	local RootPos, MousePos = Root.Position, Mouse.Hit.Position
	Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
	
	local Player = game:GetService('Players').LocalPlayer
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild('Humanoid')
	
	task.wait(0.06)
	debounce = false
end
end
end)

local animIdle = Instance.new("Animation")
animIdle.AnimationId = "http://www.roblox.com/Asset?ID=11723362107" 
local animEquip = Instance.new("Animation")
animEquip.AnimationId = "http://www.roblox.com/Asset?ID=11723279757" 
local track
local trackequip
tool.Equipped:Connect(function()
	debounce = true
	if player.Character:FindFirstChild("Steyr M") ~= nil then
	script.Parent.Origin.M2equip:Play()
	track = script.Parent.Parent.Humanoid:LoadAnimation(animEquip)
	track.Priority = Enum.AnimationPriority.Action4
	track.Looped = false
	track:Play()
	task.wait(0.1)
	track = script.Parent.Parent.Humanoid:LoadAnimation(animIdle)
	track.Priority = Enum.AnimationPriority.Action2
	track.Looped = true
	track:Play()
end
	debounce = false
end)

debounce = false

tool.Unequipped:Connect(function()
	lookat = false
	if track then
		track:Stop()
	end
end)

-- 11702743666 Fire
-- 11689904047 Idle
-- 11702935791 Equip

---------Server side script, inside of serverscriptservice

local Steyrshoot = game.ReplicatedStorage.Steyrfire
local debris = game:GetService("Debris")

local range = 75

local BulletsFolder = game.Workspace.Bullets

local uimoney = game.ReplicatedStorage:WaitForChild("MoneyGui") --get screengui

game.Players.PlayerAdded:Connect(function(p)
	localplayer = p
	playername = p.Name
end)

local function CreateBeam(origin, direction)
	local midpoint = origin + direction/2
	
	local part = Instance.new("Part")
	part.Parent = BulletsFolder
	part.Anchored = true
	part.CanCollide = false
	
	part.Material = Enum.Material.Neon
	part.Color = Color3.new(1, 1, 0)
	
	part.CFrame = CFrame.new(midpoint, origin)
	part.Size = Vector3.new(0.1, 0.1, direction.magnitude)
	
	debris:AddItem(part, 0.08)
end


Steyrshoot.OnServerEvent:Connect(function(player, mousePos, originPos)
	local direction = (mousePos - originPos).Unit * range

	local result = workspace:Raycast(originPos, direction)

	if result then
		print(result.Instance.Name)
		local character = result.Instance.Parent
		local humanoid = character:FindFirstChild("Zombie")

		print(result.Instance.Parent.Name)

		if result.Instance.Parent.Name == "Zombie" then
			if result.Instance.Parent.Zombie.Health > 0 then
				if result.Instance.Name == "Head" and humanoid ~= player.Character.Humanoid then
					humanoid:TakeDamage(15)
					print("Headshot!")
					workspace[playername]["Steyr M"].Origin.Headshot1:Play()
					if result.Instance.Parent.Class.Value == 1 and humanoid.Health < 1 then
						print("killed a zombie!")
						localplayer.leaderstats.money.Value = localplayer.leaderstats.money.Value + 10
						print(localplayer.leaderstats.money.Value)
						local moneyGui = uimoney:Clone()
						moneyGui.Parent = localplayer.PlayerGui
						moneyGui.TextLabel.Text = "+10$"
						task.wait(0.3)
						moneyGui:Destroy()
					end
					if result.Instance.Parent.Class.Value == 2 and humanoid.Health < 1 then
						print("killed a crawler!")
						localplayer.leaderstats.money.Value = localplayer.leaderstats.money.Value + 20
						print(localplayer.leaderstats.money.Value)
						local moneyGui = uimoney:Clone()
						moneyGui.Parent = localplayer.PlayerGui
						moneyGui.TextLabel.Text = "+20$"
						task.wait(0.3)
						moneyGui:Destroy()
					end
					if result.Instance.Parent.Class.Value == 3 and humanoid.Health < 1 then
						print("killed a zombie!")
						localplayer.leaderstats.money.Value = localplayer.leaderstats.money.Value + 125
						print(localplayer.leaderstats.money.Value)
						local moneyGui = uimoney:Clone()
						moneyGui.Parent = localplayer.PlayerGui
						moneyGui.TextLabel.Text = "+125$"
						task.wait(0.3)
						moneyGui:Destroy()
					end
				end

				if humanoid and humanoid ~= player.Character.Humanoid then
					humanoid:TakeDamage(10)
					if result.Instance.Parent.Class.Value == 1 and humanoid.Health < 1 then
						print("killed a zombie!")
						localplayer.leaderstats.money.Value = localplayer.leaderstats.money.Value + 10
						print(localplayer.leaderstats.money.Value)
						local moneyGui = uimoney:Clone()
						moneyGui.Parent = localplayer.PlayerGui
						moneyGui.TextLabel.Text = "+10$"
						task.wait(0.3)
						moneyGui:Destroy()
					end
					if result.Instance.Parent.Class.Value == 2 and humanoid.Health < 1 then
						print("killed a crawler!")
						localplayer.leaderstats.money.Value = localplayer.leaderstats.money.Value + 20
						print(localplayer.leaderstats.money.Value)
						local moneyGui = uimoney:Clone()
						moneyGui.Parent = localplayer.PlayerGui
						moneyGui.TextLabel.Text = "+20$"
						task.wait(0.3)
						moneyGui:Destroy()
					end
					if result.Instance.Parent.Class.Value == 3 and humanoid.Health < 1 then
						print("killed a zombie!")
						localplayer.leaderstats.money.Value = localplayer.leaderstats.money.Value + 125
						print(localplayer.leaderstats.money.Value)
						local moneyGui = uimoney:Clone()
						moneyGui.Parent = localplayer.PlayerGui
						moneyGui.TextLabel.Text = "+125$"
						task.wait(0.3)
						moneyGui:Destroy()
					end
				end
			end
		else 
			print("Missed")
		end
	end
	CreateBeam(originPos, direction)
end)

All copies of your gun are sharing the same RemoteEvent. You should have the event be located in the tool instead, so each gun gets a unique one.

I put the remote event under the tool and it works in singleplayer (as it did before), but now the raycast doesnt show up in multiplayer at all on for either player.
(Also, the ammotable reloads are still connected)

This is what I changed the serverscript to:

local debris = game:GetService("Debris")

local range = 75

local BulletsFolder = game.Workspace.Bullets

local uimoney = game.ReplicatedStorage:WaitForChild("MoneyGui") --get screengui

game.Players.PlayerAdded:Connect(function(p)
	localplayer = p
	playername = p.Name
	print(playername)
end)


task.wait(2)
Steyrshoot = localplayer.Backpack:WaitForChild("Steyr M").Steyrfire


local function CreateBeam(origin, direction)
	local midpoint = origin + direction/2
	
	local part = Instance.new("Part")
	part.Parent = BulletsFolder
	part.Anchored = true
	part.CanCollide = false
	
	part.Material = Enum.Material.Neon
	part.Color = Color3.new(1, 1, 0)
	
	part.CFrame = CFrame.new(midpoint, origin)
	part.Size = Vector3.new(0.1, 0.1, direction.magnitude)
	
	debris:AddItem(part, 0.08)
end


Steyrshoot.OnServerEvent:Connect(function(player, mousePos, originPos)
	local direction = (mousePos - originPos).Unit * range

	local result = workspace:Raycast(originPos, direction)

	if result then
		print(result.Instance.Name)
		local character = result.Instance.Parent
		local humanoid = character:FindFirstChild("Zombie")

		print(result.Instance.Parent.Name)

		if result.Instance.Parent.Name == "Zombie" then
			if result.Instance.Parent.Zombie.Health > 0 then
				if result.Instance.Name == "Head" and humanoid ~= player.Character.Humanoid then
					humanoid:TakeDamage(15)
					print("Headshot!")
					workspace[playername]["Steyr M"].Origin.Headshot1:Play()
					if result.Instance.Parent.Class.Value == 1 and humanoid.Health < 1 then
						print("killed a zombie!")
						localplayer.leaderstats.money.Value = localplayer.leaderstats.money.Value + 10
						print(localplayer.leaderstats.money.Value)
						local moneyGui = uimoney:Clone()
						moneyGui.Parent = localplayer.PlayerGui
						moneyGui.TextLabel.Text = "+10$"
						task.wait(0.3)
						moneyGui:Destroy()
					end
					if result.Instance.Parent.Class.Value == 2 and humanoid.Health < 1 then
						print("killed a crawler!")
						localplayer.leaderstats.money.Value = localplayer.leaderstats.money.Value + 20
						print(localplayer.leaderstats.money.Value)
						local moneyGui = uimoney:Clone()
						moneyGui.Parent = localplayer.PlayerGui
						moneyGui.TextLabel.Text = "+20$"
						task.wait(0.3)
						moneyGui:Destroy()
					end
					if result.Instance.Parent.Class.Value == 3 and humanoid.Health < 1 then
						print("killed a zombie!")
						localplayer.leaderstats.money.Value = localplayer.leaderstats.money.Value + 125
						print(localplayer.leaderstats.money.Value)
						local moneyGui = uimoney:Clone()
						moneyGui.Parent = localplayer.PlayerGui
						moneyGui.TextLabel.Text = "+125$"
						task.wait(0.3)
						moneyGui:Destroy()
					end
				end

				if humanoid and humanoid ~= player.Character.Humanoid then
					humanoid:TakeDamage(10)
					if result.Instance.Parent.Class.Value == 1 and humanoid.Health < 1 then
						print("killed a zombie!")
						localplayer.leaderstats.money.Value = localplayer.leaderstats.money.Value + 10
						print(localplayer.leaderstats.money.Value)
						local moneyGui = uimoney:Clone()
						moneyGui.Parent = localplayer.PlayerGui
						moneyGui.TextLabel.Text = "+10$"
						task.wait(0.3)
						moneyGui:Destroy()
					end
					if result.Instance.Parent.Class.Value == 2 and humanoid.Health < 1 then
						print("killed a crawler!")
						localplayer.leaderstats.money.Value = localplayer.leaderstats.money.Value + 20
						print(localplayer.leaderstats.money.Value)
						local moneyGui = uimoney:Clone()
						moneyGui.Parent = localplayer.PlayerGui
						moneyGui.TextLabel.Text = "+20$"
						task.wait(0.3)
						moneyGui:Destroy()
					end
					if result.Instance.Parent.Class.Value == 3 and humanoid.Health < 1 then
						print("killed a zombie!")
						localplayer.leaderstats.money.Value = localplayer.leaderstats.money.Value + 125
						print(localplayer.leaderstats.money.Value)
						local moneyGui = uimoney:Clone()
						moneyGui.Parent = localplayer.PlayerGui
						moneyGui.TextLabel.Text = "+125$"
						task.wait(0.3)
						moneyGui:Destroy()
					end
				end
			end
		else 
			print("Missed")
		end
	end
	CreateBeam(originPos, direction)
end)

I think you need two different remote events then. One for things specific to that gun and player, the firing and reloading info, and one for things all players should see, like the bullet trails.

Not quite sure what you mean by that.
The only thing the remove event does is fire the ray + beam and create the yellow part to visualize it (and change the money, but thats unrelated), all the ammo is handled in the local script including the ammotable so it should only affect the tool its under, no?

Being a LocalScript ensures it only affects that player but not if that info goes through a remote. You can track ammo local only if you wish though.

Ive got to the point where the 2nd players (the one who joins second) weapon works fine, but the first players weapon will not fire the remoteevent. It also doesnt print their playername under the playeradded function in the server script. Im pretty sure its just overwriting the localplayer when the new player is added, so how do I get around this? I need to reference the localplayer in the server script without PlayerAdded?

Incase anyone else has this problem, I fixed it!

use a remote event called something like “WhoFired” to get the localplayers name every time the weapon is fired, instead of using PlayerAdded to set the localplayer in the server script so that it doesnt get overwritten when a new player joins.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.