Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Touched Part only works for the latest player that joins?

So, I've been working on a game with some friends and we're trying to do something simple. But there is an issue with the selling part of the game. If your in a solo server it works just fine, but in multiplayer server, whenever you hit the sell part it only sells the latest person that joins items. This is a pretty big issue here, and i would appreciate it if someone could look at my code and maybe make a few tweaks.

The most important part is between lines 49 and 66.



-- References --
local SSStorage = game:GetService("ServerScriptService")
local Players = game:GetService("Players")
local RepStorage = game:GetService("ReplicatedStorage")

-- Debounce --
local db = false


-- Datastore2 --
local Datastore2 = require(SSStorage:FindFirstChild("Datastore2"))

Datastore2.Combine("DATA", "Ice", "Coins")

Players.PlayerAdded:Connect(function(player)
	
	local IceStore = Datastore2("Ice", player)
	local CoinsStore = Datastore2("Coins", player)
	
	local leaderstats = Instance.new("Folder")
	  leaderstats.Name = "leaderstats"
	
	local Ice = Instance.new("NumberValue")
	  Ice.Name = "Ice"
	  Ice.Value = IceStore:Get(0)
	  Ice.Parent = leaderstats
	
	local Coins = Instance.new("NumberValue")
	  Coins.Name = "Coins"
	  Coins.Value = CoinsStore:Get(0)
	  Coins.Parent = leaderstats
	
	
	IceStore:OnUpdate(function(GainedIce)
		
		Ice.Value = GainedIce
		
	end)
	
	CoinsStore:OnUpdate(function(GainedCoins)
		
		Coins.Value = GainedCoins
		
	end)
	
	leaderstats.Parent = player
	
	
	workspace.Sell.Touched:Connect(function(hit)
		
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if plr and db==false then
			print(plr)
			
			db = true
			CoinsStore:Increment(Ice.Value)
			IceStore:Set(0)
			wait(0.8)
			db = false
		end
		
		
	end)
	
end)

workspace.Ice.ClickDetector.MouseClick:Connect(function(player)
	
	local Ice = game.Workspace:FindFirstChild("Ice")
	local IceStore = Datastore2("Ice", player)
	
	IceStore:Increment(Ice:FindFirstChild("Value").Value)
	
end)

workspace.DarkIce.ClickDetector.MouseClick:Connect(function(player)
	
	local DarkIce = game.Workspace:FindFirstChild("DarkIce")
	local IceStore = Datastore2("Ice", player)
	
	IceStore:Increment(DarkIce:FindFirstChild("Value").Value)
	
end)

workspace.DeepIce.ClickDetector.MouseClick:Connect(function(player)
	
	local DeepIce = game.Workspace:FindFirstChild("DeepIce")
	local IceStore = Datastore2("Ice", player)
	
	IceStore:Increment(DeepIce:FindFirstChild("Value").Value)
	
end)

workspace.UnstableIce.ClickDetector.MouseClick:Connect(function(player)
	
	local UnstableIce = game.Workspace:FindFirstChild("UnstableIce")
	local IceStore = Datastore2("Ice", player)
	
	IceStore:Increment(UnstableIce:FindFirstChild("Value").Value)
	
end)

1 answer

✓ Accepted answer

Because you use it inside a PlayerAdded event, just take it out of there and you should be good.

1 comment

0

It really was that simple. I thank you a lot lmao

This discussion is preserved as it was. New answers, comments, and votes are closed.