-
What do you want to achieve?
Multiple players with guns that work independently of eachother. -
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
-
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)