When firing a remote event, it gives random values instead of the one I want

You can write your topic however you want, but you need to answer these questions:
When I touch this crystal, you get 1 crystal stat, the crystal removes itself, and it subtracts one from the starting value

When I touch a crystal, it gives a random amount, I tried adding a debounce but its not working.

https://gyazo.com/0d8228dbb2b1301a1ff7570f8052b408

There are 3 scripts to this system:

Server Remote Fire (Local, starter player scripts)

local plot = game:GetService("Workspace"):WaitForChild("Plot")
local debounce = false
local waittime = 5
while task.wait(0.1) do
	for _, crystal in pairs(plot:GetChildren()) do
		if crystal.Name == "BasicCrystal" then
			crystal.Prim.Touched:Connect(function(PartTouched) 
				if PartTouched.Parent.Name == game.Players.LocalPlayer.Name then
					debounce = true
					game.ReplicatedStorage.Remotes.CrystalTouchNormal:FireServer(game.Players.LocalPlayer)
					crystal:Destroy()
					task.wait(waittime)
					debounce = false
				end
			end)
		end
	end
end

Crystal Giver (Server, serverscriptservice)

game.ReplicatedStorage.Remotes.CrystalTouchNormal.OnServerEvent:Connect(function(plr)
	local crystal = plr:WaitForChild("Data"):WaitForChild("Runes"):WaitForChild("NormalCrystal")
	crystal.Value += 1
	game.ReplicatedStorage.Remotes.CrystalRemove:FireClient(plr)
end)

Crystal Spawner (Local, starterplayerscripts)

local plot = game.Workspace:WaitForChild("Plot")
local crystals = game.ReplicatedStorage.Crystals

local luck = require(game.ReplicatedStorage:FindFirstChild("Luck"))

normal = crystals.BasicCrystal:Clone()
shiny = crystals.ShinyCrystal:Clone()
dm = crystals.DarkMatterCrystal:Clone()


local chances = {
	{name = "BasicCrystal", weight = 50},
	{name = "ShinyCrystal",  weight = 30},
	{name = "DarkMatterCrystal", weight = 20},
}

local start = 0
local max = 25

function chooseCrystal()
	local chosen = luck.SelectRarity(chances)

	local newclone = crystals[chosen.name]:Clone()
	newclone.Parent = game.Workspace.Plot
	newclone.Prim.Position = Vector3.new(math.random(-11.75,50.25), math.random(5,5), math.random(-109.25, -41.75))
	start += 1
end

game.ReplicatedStorage.Remotes.CrystalRemove.OnClientEvent:Connect(function()
	if start <=0 then
		return
	end
	start -= 1
	print(start)
end)

while task.wait(1) do
	if start == max then
		return
	end
	chooseCrystal()
end
1 Like

There are 3 problems in the Server Remote Fire script:

  • There’s a memory leak because the Touched connection is being created inside of an infinite loop
  • You have 1 debounce for every Prim instead of a debounce for each Prim
  • You’re not checking the value of the debounce

This should work to fix those issues:

local plot = game:GetService("Workspace"):WaitForChild("Plot")
local waittime = 5

for _, crystal in pairs(plot:GetChildren()) do
	if crystal.Name == "BasicCrystal" then
		local debounce = false

		crystal.Prim.Touched:Connect(function(PartTouched)
			if debounce then return end

			if PartTouched.Parent.Name == game.Players.LocalPlayer.Name then
				debounce = true
				game.ReplicatedStorage.Remotes.CrystalTouchNormal:FireServer(game.Players.LocalPlayer)
				crystal:Destroy()
				task.wait(waittime)
				debounce = false
			end
		end)
	end
end
1 Like

It no longer works now, does it have anything to do with the return end?

1 Like

Actually since it’s a LocalScript it might be that the for loop is running before the crystals have finished loading, so this is what you need to do to fix this issue:

local plot = game:GetService("Workspace"):WaitForChild("Plot")
local waittime = 5

local function onCrystalAdded(crystal)
	if crystal.Name == "BasicCrystal" then
		local debounce = false

		crystal.Prim.Touched:Connect(function(PartTouched)
			if debounce then return end

			if PartTouched.Parent.Name == game.Players.LocalPlayer.Name then
				debounce = true
				game.ReplicatedStorage.Remotes.CrystalTouchNormal:FireServer(game.Players.LocalPlayer)
				crystal:Destroy()
				task.wait(waittime)
				debounce = false
			end
		end)
	end
end

for _, crystal in pairs(plot:GetChildren()) do onCrystalAdded(crystal) end

plot.ChildAdded:Connect(onCrystalAdded)
2 Likes

This doesnt get past the second line. Only prints wait time and plot, function is broken

1 Like

I’ll re-write it in my preferred syntax and I’ll edit this reply when I’m done, but in all honesty I promise that the script I provided should have worked correctly (although replacing crystal.Prim.Touched with crystal:WaitForChild("Prim").Touched might fix it if you’d like to try it out before I write the new version)

1 Like

changing it to waitforchild did not work

1 Like

I’m so sorry I forgot the little line at the bottom :sob: this worked flawlessly, thank you!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.