Array Event not working

Hey! To handle the coins in my game, I wrote (with some help) this script. However it does not function. No errors are outputed. I am not sure what the problem in the script is.

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local CoinFol = game.Workspace.Coins

local r = math.rad 

function RotateCoin()
	for _, coin in pairs(CoinFol:GetChildren()) do
		coroutine.wrap(function()
			while task.wait() do 
				coin.CFrame *= CFrame.Angles(r(0), r(1), r(0))
			end
		end)
	end
end


function CoinTouched(Coin,hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local char = hit.Parent 
		local coinV = require(game.ServerScriptService.FastGet)(player.UserId,"Coin")
		if Coin:WaitForChild("Particle",1) then
			coinV.Value += 10
		else
			coinV.Value += 2
		end
		local XPV = require(game.ServerScriptService.FastGet)(player.UserId,"Xp")
		XPV.Value += 20
		Coin:Destroy()
	end
end


function ConnectCoins()
	for _, coin in pairs(CoinFol) do
		coin.Touched:Connect(function(p2) CoinTouched(coin,p2) end)
	end
end

function LoopC()
	while true do
		wait(0.5)
		RotateCoin()
	end
end

coroutine.wrap(LoopC)

I believe the problem is when you require a ModuleScript.

 local XPV = require(game.ServerScriptService.FastGet) 

You can’t add (player.UserId,"Xp") after that.

(that goes for whenever using require, same with your coinV variable)

It is a function, adding the values on to the PlayerData does work, so that is not the cause of the problem anyways.

Since your script looks like this:

local coinV = require(game.ServerScriptService.FastGet)(player.UserId,"Coin")
		if Coin:WaitForChild("Particle",1) then
			coinV.Value += 10

Your ModuleScript looks like this:

local FastGet = {}

FastGet.Value = 0

return FastGet

You’re changing the value that is inside of the Module. Is this intentional?

This is that module script, it is not the cause of the Issue.

local module = function (playerID,aspect)
	local PFol = game.ReplicatedStorage.PlayerData[playerID]
	if PFol then
		local varS = PFol[aspect]
		if varS then
			return varS
		end
	end
end

return module

.Value is because it is an IntValue object.

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local CoinFol = game.Workspace.Coins

local r = math.rad 

function RotateCoins()
   coroutine.wrap(function()
      while task.wait() do
         for _, coin in pairs(CoinFol:GetChildren()) do
           coin.CFrame *=CFrame.Angles(r(0), r(1), r(0))
	end
end


function CoinTouched(Coin,hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local char = hit.Parent 
		local coinV = require(game.ServerScriptService.FastGet)(player.UserId,"Coin")
		if Coin:WaitForChild("Particle",1) then
			coinV.Value += 10
		else
			coinV.Value += 2
		end
		local XPV = require(game.ServerScriptService.FastGet)(player.UserId,"Xp")
		XPV.Value += 20
		Coin:Destroy()
	end
end

for _, coin in pairs(CoinFol) do 
   coin.Touched:Connect(function(p2) 
      CoinTouched(coin,p2)
   end)
end

RotateCoins()

Привет, у тебя встречаются 3 проблемы:

  1. Неправильное использование функций.
  2. Ты забыл вызвать некоторые функции.
  3. Неправильное использование циклов.

Я отредактировал код, но возможно что-то упустил. Если проблема останется, то пиши мне. Также, не забывай смотреть в Output.

If your module is working, then try changing the value inside of the module.

local module = function (playerID,aspect)
	local PFol = game.ReplicatedStorage.PlayerData[playerID]
	if PFol then
		local varS = PFol[aspect]
		if varS then
           varS.Value += 10
			--return varS
		end
	end
end

return module

Спасибо за помощь, но это не русская ветка.

Like I am telling you, the Error is not with the Module! It is with the detection of hit and rotation of coin.

I know, which is why as I said above to try changing the value of your item inside the module. You’re getting the item inside the module so change it there too.

The Module does not need changing, it works completely fine.

I was telling him that this isn’t a Russian thread.

I see that you’re not understanding what I’m saying, that’s fine.

Good luck on finding a solution.

Thanks PAGO for your help on this thread and other of my threads, I really appreciate it! :heart:

I think you misunderstand the purpose of that ModuleScript. It is just to return the IntValue that contains the variable. Example: Coin, Xp or Token. Not to add only 10 to the value, like what you added to it. If all the variables (Token,Coin,Xp,ect.) only needed to be added by 10, then your idea would work. ModuleScripts are for multiple scripts. I suggest you look more into the function of Module Scripts.

1 Like

I apologize, I see my error now. I am mentally drained right now… glad you found the solution!

1 Like

The coins in CoinFol spawn in overtime, so I had to do a while true do in there. But that excelirated the Coins to rotate very quickly and it caused massive XP and COIN dupes.

Just to explain it further, the purpose of the ModuleScript is to go through that player file and return the IntValues, whom values could then be changed in the Script
image
It just returns the IntValue objects, whom Values are changed by the ServerScript requesting it. Which is then saved into the Player’s Datastore when they leave and loads when the player joins.

1 Like

that fixes the Rotation issue, but still dupes the coin values many times. (Coin meant to give 2 coins, gave around 20)

Should I make the coins have script to Manage hit detection and coin giving and have the rotations managed in the central script?