I made a Gun Game with Guns (obviously), and since it’s a new genre of game, I can’t quite script everything right.
^ The Guns Menu
When more than 1 player join the game, the Guns Menu will show the Guns for one player, and if they both join at the same time, for one player the guns will work, for the others not
local player = game.Players.LocalPlayer
local gun = script.Parent.Parent.ViewportObject.Value
local debounce = false
script.Parent.MouseButton1Click:Connect(function()
local gunInPlayer = player.Character:FindFirstChild(gun.Name) or player.Backpack:FindFirstChild(gun.Name)
script.PopSound2:Play()
if debounce == false then
debounce = true
script.Parent.Parent.Title.TextColor3 = Color3.fromRGB(255, 0, 4)
script.Parent.Parent.Title.FontFace.Weight = Enum.FontWeight.Bold
script.Parent.Parent.Title.Text = "EQUIPPED"
game.ReplicatedStorage.Remotes.FireGunToServer:FireServer(gun, debounce)
elseif debounce == true then
debounce = false
script.Parent.Parent.Title.TextColor3 = Color3.fromRGB(255, 255, 255)
script.Parent.Parent.Title.FontFace.Weight = Enum.FontWeight.ExtraLight
script.Parent.Parent.Title.Text = gun.Name
game.ReplicatedStorage.Remotes.UnequipGun:FireServer(gun, debounce)
end
end)
^ The local script responsible for telling the server to give the player that respective gun (Note: the guns are not put in manually, there is a script that takes the guns from the folder that holds the guns and make a button with the script I put above)
game.ReplicatedStorage.Remotes.FireGunToServer.OnServerEvent:Connect(function(player, hat, debounce)
local hatClone = hat:Clone()
hatClone.Parent = player.Backpack
end)
game.ReplicatedStorage.Remotes.UnequipGun.OnServerEvent:Connect(function(player, hat, debounce)
local hatInPlayer = player.Character:FindFirstChild(hat.Name) or player.Backpack:FindFirstChild(hat.Name)
if hatInPlayer then
hatInPlayer:Destroy()
end
end)
^ The script (located in ServerScriptService) that gives the players the guns they chose to have)
^ The necessary scripts/events/modules/values for each gun
wait()
local tool = script.Parent
local player = game.Players.LocalPlayer
local hum = player.Character:FindFirstChildOfClass("Humanoid")
local runService = game:GetService("RunService")
local mouse = player:GetMouse()
local idleanimation = hum:LoadAnimation(script.Idle)
local equipanimation = hum:LoadAnimation(script.Equip)
script.Parent.Equipped:Connect(function(Mouse)
script.Parent.BodyAttach.EquipSound:Play()
game.ReplicatedStorage.ConnectM6D:FireServer(tool.BodyAttach)
equipanimation:Play()
equipanimation.Priority = Enum.AnimationPriority.Action4
idleanimation.Priority = Enum.AnimationPriority.Action
idleanimation.Looped = true
idleanimation:Play()
end)
runService.RenderStepped:Connect(function()
if script.Parent.Parent == game.Players.LocalPlayer.Backpack then
idleanimation:Stop()
equipanimation:Stop()
game.ReplicatedStorage.DisconnectM6D:FireServer()
end
end)
^ The local script that tells the server to connect the gun’s Motor6d with the player’s torso (TUTORIAL SCRIPT)
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
local m6d = Instance.new("Motor6D")
local charTorso = char:FindFirstChild("Torso") or char:FindFirstChild("LowerTorso")
m6d.Name = "ToolGrip"
m6d.Parent = charTorso
end)
end)
game.ReplicatedStorage.ConnectM6D.OnServerEvent:Connect(function(plr, location)
local char = plr.Character
local charTorso = char:FindFirstChild("Torso") or char:FindFirstChild("LowerTorso")
charTorso.ToolGrip.Part0 = charTorso
charTorso.ToolGrip.Part1 = location
end)
^ The server script that actually does the action I mentioned above (also located in ServerScriptService)
If anybody could help, I tried to fix it myself but I don’t seem to know the reason why this would be happening
THANKS!