Im making a dalgona game.
The issue is that when I play first time everything is fine. But when I play second time , server receives signal 2 times.
Server Script:
-- Services
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
-- Folders
local figuresFolder = replicatedStorage.Figures
local spotsFolder = workspace:WaitForChild("spots")
-- Remote Events
local cookiegameEvent = replicatedStorage:WaitForChild("cookiegame")
local cookiegameUpdate = replicatedStorage:WaitForChild("cookiegameUpdate")
-- Spot States
local spotStates = {}
------------------------
------------SIXTH ACTION------------------------------------------------
local function endGame(player , spot , won)
local humanoid = player.Character:FindFirstChild("Humanoid")
humanoid.WalkSpeed = 16
humanoid.JumpHeight = 7.5
if won then
cookiegameUpdate:FireClient(player)
else
cookiegameUpdate:FireClient(player)
end
spotStates[spot] = {isOccupied = false , currentPlayer = nil}
spot.ProximityPrompt.Enabled = true
spot:FindFirstChildWhichIsA("Model"):Destroy()
end
----------------------FIFTH ACTION----------------------------------------------------
local function handlePlayerClick(player, hitPart , spot)
local gameState = spotStates[spot].gameState
if hitPart:GetAttribute("Shape") and not hitPart:GetAttribute("clicked") then
hitPart:SetAttribute("clicked" , true)
gameState.partsClicked = gameState.partsClicked + 1
hitPart.Transparency = 0
hitPart.Material = Enum.Material.Neon
hitPart.Color = Color3.new(0.133333, 1, 0)
if gameState.partsClicked == hitPart.Parent.Parent:GetAttribute("numbreak") then
print("u won!")
endGame(player , spot , true)
end
elseif not hitPart:GetAttribute("Shape") then
gameState.cracksCount = gameState.cracksCount + 1
if gameState.cracksCount == 3 then
print("u lost!")
endGame(player , spot , false)
end
end
end
------------------------SECOND ACTION-----------------------------------------------
local function startGameForPlayer(player , spot)
local gameState = {partsClicked = 0 , cracksCount = 0 , isPlaying = true}
spotStates[spot] = {isOccupied = true , currentPlayer = player , gameState = gameState}
spot.ProximityPrompt.Enabled = false
local character = player.Character
if character and character:FindFirstChild("HumanoidRootPart") then
local humanoid = character:FindFirstChild("Humanoid")
local hrp = character:FindFirstChild("HumanoidRootPart")
local randomFigure = figuresFolder:GetChildren()[math.random(1 , #figuresFolder:GetChildren())]:Clone()
randomFigure.Parent = spot
randomFigure:SetPrimaryPartCFrame(spot.CFrame + Vector3.new(0, 1.15 ,0))
local offSetDistance = -3
local newPosition = spot.CFrame.Position - (spot.CFrame.LookVector * offSetDistance)
hrp.CFrame = CFrame.new(newPosition , newPosition + -spot.CFrame.LookVector)
humanoid.WalkSpeed = 0
humanoid.JumpHeight = 0
local cameraPart = spot:FindFirstChild("cameraPart")
cookiegameEvent:FireClient(player , cameraPart , randomFigure)
------------------FOURTH ACTION--------------------------------------
cookiegameUpdate.OnServerEvent:Connect(function(player , hitPart)
print("got on server!")
if spotStates[spot].currentPlayer == player then
print("connecting HPC function")
handlePlayerClick(player , hitPart , spot)
end
end)
end
print(spotStates[spot])
end
---------------------FIRST ACTION-----------------------------------------
for i, spot in ipairs(spotsFolder:GetChildren()) do
spotStates[spot] = {isOccupied = false , currentPlayer = "Player?"}
local prompt = spot:WaitForChild("ProximityPrompt")
prompt.Triggered:Connect(function(player)
if not spotStates[spot].isOccupied then
startGameForPlayer(player , spot)
end
end)
end
Client
-- Services
local replicatedStorage = game:GetService("ReplicatedStorage")
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
-- Remote Events
local cookiegameEvent = replicatedStorage:WaitForChild("cookiegame")
local cookiegameUpdate = replicatedStorage:WaitForChild("cookiegameUpdate")
local heartbeatConnection
local mouseClickConnection
-----------------THIRD ACTION------------------------------------------------------
cookiegameEvent.OnClientEvent:Connect(function(cameraPart , Figure)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local needle = replicatedStorage.Needle:Clone()
needle.Parent = workspace
local curCamera = workspace.CurrentCamera
curCamera.CameraType = Enum.CameraType.Scriptable
curCamera.CFrame = cameraPart.CFrame
local function handleMouseClick()
local unitray = curCamera:ScreenPointToRay(mouse.X , mouse.Y)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {needle}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local raycastResult = workspace:Raycast(unitray.Origin , unitray.Direction * 50 , raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
print("FIRING TO SERVER !")
cookiegameUpdate:FireServer(hitPart)
end
end
mouseClickConnection = mouse.Button1Down:Connect(handleMouseClick)
end)
-----------------LAST END GAME ACTION-------------------------
cookiegameUpdate.OnClientEvent:Connect(function(player)
local curCamera = workspace.CurrentCamera
curCamera.CameraType = Enum.CameraType.Custom
if mouseClickConnection then
mouseClickConnection:Disconnect()
mouseClickConnection = nil
end
end)
As u can see there is a print function on line 99 in server script.
When player plays it second time it receives and prints it 2 times and so on , play 3rd time it prints 3 times.
How can I fix it?