Revoir mon script d’objet de jeu car il a un bug

No idea if it’s a logic bug or not, thank you!

These are the two codes

--MODULEE
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ReplicatedContents = ReplicatedStorage.Contents
local ServerStorage = ServerStorage.Contents

local Events = ReplicatedStorage.Events
local Package = ReplicatedStorage.Package

local Guns = require(ServerStorage.Gun.Guns)
local Gun = require(ServerStorage.Gun.Gun)
local Player = require(ServerStorage.Player.Player)

local Enums = require(ReplicatedContents.Enums.Enums)

local gunBindable = Events.GunBindable
local gameEvent = Events.GameEvent
local notificationEvent = Events.NotificationEvent

local Maps = Package.Maps

local Map = workspace.Map
local Hub = workspace.Hub

local HubParts = Hub.Parts
local hubBasePlate = HubParts.Baseplate

local GameObject = {}
GameObject.__index = GameObject


local MAX_COINS_PER_WIN = 25

local function setData(player)
	local objectPlayer = Player.find(player.UserId)
	local coinsMultiplier = objectPlayer:getValue("Coins_Multiplier")

	objectPlayer:setValue("Coins", (MAX_COINS_PER_WIN * coinsMultiplier))
	objectPlayer:setValue("Total_Wins", 1)
end

function GameObject.new(map, players)
	local self = setmetatable({}, GameObject)
	
	self.map = Maps:FindFirstChild(map):Clone()
	self.time = 180
	self.players = {}
	
	return self
end

local function setCharacter(character)
	local humanoid = character.Humanoid
	humanoid.Health = 50
	humanoid.MaxHealth = 50
end

function GameObject:spawnPlayers()
	local characters = {}
	local spawnParts = self.map.SpawnParts:GetChildren():: {BasePart}
	for _, userId: number in self.players do
		local player = Players:GetPlayerByUserId(userId)
		
		local character = player.Character

		if not character then return end
		
		gameEvent:FireClient(player, Enums.GameEvent.SetUp)
		setCharacter(character)
		self:setGun(player)
		
		table.insert(characters, character)
	end

	for i, character: Model in characters do
		local humanoid = character:FindFirstChild("Humanoid"):: Humanoid
		
		character:PivotTo(CFrame.new(spawnParts[i].Position + Vector3.new(0, 3, 0)))
		
		humanoid.Died:Connect(function()
			local player = Players:GetPlayerFromCharacter(character)
			
			self:removePlayer(player)
		end)
		
		Players.PlayerRemoving:Connect(function(player)
			self:removePlayer(player)
		end)
	end
end

function GameObject:setGun(player: Player)
	gunBindable:Fire(player, Enums.Action.Set)
end

function GameObject:setMap()
	self.map.Parent = Map
end

function GameObject:removePlayer(player: Player)
	local character = player.Character
	local find = table.find(self.players, player.UserId)
	
	print(player.UserId, "QUAND TU FIND QUAND LE JOUEUR QUITTE")
	if find then
		table.remove(self.players, find)
	end
	
	print(character, "QUAND TU FIND QUAND LE JOUEUR QUITTE")
	
	if character then
		character:PivotTo(CFrame.new((hubBasePlate.Position + Vector3.new(0, 6, 0)) / 2))
	end
	
	gunBindable:Fire(player, Enums.Action.Remove)
	gameEvent:FireClient(player, Enums.GameEvent.Remove)
end

function GameObject:removeMap()
	self.map:Destroy()
end

function GameObject:start(players: {Player})
	local timeUp = false
	
	for _, player in players do
		table.insert(self.players, player.UserId)
	end
	
	self:spawnPlayers()
	self:setMap()
	
	task.spawn(function()
		task.wait(self.time)
		timeUp = true
	end)
	
	repeat
		task.wait()
	until #self.players == 1 or timeUp
		
	return self:stop()
end

function GameObject:stop()
	self:removeMap()
	
	for _, userId in self.players do
		local player = Players:GetPlayerByUserId(userId)
		
		self:removePlayer(player)
		setCharacter(player.Character)
		setData(player)
		
		notificationEvent:FireAllClients(player.Name.." Win !", Color3.new(1, 0.694118, 0.203922))
	end
	
	return self:remove()
end

function GameObject:remove()
	table.clear(self)
	setmetatable(self, nil)
	
	return true
end

return GameObject
--GAME LOCAL SCRIPT

if not game:IsLoaded() then game.Loaded:Wait() end

local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenSercice = game:GetService("TweenService")

local Contents = ReplicatedStorage.Contents
local Events = ReplicatedStorage.Events

local Controllers = Contents.Controllers

local SizeController = require(Controllers.SizeController)
local SprintController = require(Controllers.SprintController)
local Enums = require(Contents.Enums.Enums)

local gameEvent = Events.GameEvent

local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local playerUi = playerGui:FindFirstChild("PlayerUi")


local arms = {"Left Arm", "Right Arm"}

local SETUP_ARMS_SIZE = Vector3.new(0.75, 3, 0.75)
local REMOVE_ARMS_SIZE = Vector3.new(1, 2, 1)


local function onSprint(actionName, inputState: Enum.UserInputState)
	if inputState == Enum.UserInputState.Begin then
		SprintController:play()
	else
		SprintController:stop()
	end
end

local function setUp()
	playerUi.Enabled = false
	player.CameraMode = Enum.CameraMode.LockFirstPerson
	SizeController:adjustSize(arms, SETUP_ARMS_SIZE)
	
	SprintController:setSprintState(true)
	ContextActionService:BindAction("Sprint", onSprint, true, Enum.KeyCode.LeftShift)
end

local function remove()
	playerUi.Enabled = true
	player.CameraMode = Enum.CameraMode.Classic
	SizeController:adjustSize(arms, REMOVE_ARMS_SIZE)
	
	SprintController:setSprintState(false)
	ContextActionService:UnbindAction("Sprint")
end

gameEvent.OnClientEvent:Connect(function(action: Enums.Enums)
	if action == Enums.GameEvent.SetUp then
		setUp()
	elseif action == Enums.GameEvent.Remove then
		remove()
	end
end)

¿Qué dans le mundo ты выложил?

1 Like