What do you want to achieve? Keep it simple and clear!
I’m working on a simulator in which you collect parts and get faster.
What is the issue? Include screenshots / videos if possible!
When I fire to the server on the client, nothing prints or happens.
Server
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")
--Imporant folders
local Modules = ReplicatedStorage.Modules
local ServerModules = ServerStorage.ServerModules
local Events = ReplicatedStorage.Events
local SpiritFolder = workspace.SpiritFolder
--Modules
local ballModule = require(ServerModules.Ball)
local playerStatsModule = require(ServerModules.PlayerStats)
--Instances
local spawnArea = workspace.SpawnArea
--Tables
Players.PlayerAdded:Connect(function(player)
playerStatsModule.new(player)
end)
while task.wait(1) do
if #SpiritFolder:GetChildren() < 10 then
local randomSize = math.random(2,5)
local randomPosition = spawnArea.Position + Vector3.new(math.random(-spawnArea.Size.X / 2,spawnArea.Size.X / 2),spawnArea.Position.Y,math.random(-spawnArea.Size.Z / 2,spawnArea.Size.Z / 2))
local randomColor = Color3.fromRGB(math.random(0,255),math.random(0,255),math.random(0,255))
local randomSize = Vector3.new(randomSize,randomSize,randomSize)
local randomSpirit = ballModule.Create(randomColor,randomSize,randomPosition,SpiritFolder)
Debris:AddItem(randomSpirit,20)
if randomSize.X >= 4 then
randomSpirit:SetAttribute("Spirit",5)
else
randomSpirit:SetAttribute("Spirit",2)
end
end
end
Events.Collect.OnServerEvent:Connect(function(player,instance,spirit)
print("3 UR ON SERVER")
player.leaderstats.Spirits.Value += spirit
instance:Destroy()
end)
Client
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage.Events
local Modules = ReplicatedStorage.Modules
CollectionService:GetInstanceAddedSignal("Spirit"):Connect(function(instance)
instance.Touched:Connect(function()
if not instance:GetAttribute("Collected") then
instance:SetAttribute("Collected",true)
Events.Collect:FireServer(instance,instance:GetAttribute("Spirit"))
end
end)
end)
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried messing with getinstanceaddedsignal and collectionservice but I’m starting to think this has to with something I don’t know about.
I just realized that, I also have one last question, should I have a pairs loop with my getinstanceaddedsignal on the client? After I realized my mistake some parts were still not being recognized but when I added a pairs loop, looping through all of the tags of the parts, they were all collectable. If so, why should I?
Not quite sure what you mean, but: the difference between pairs and ipairs is ipairs must have numerical indices. So 1,2,3 etc. The i in ipairs stands for index. You can use ipairs, but only for arrays. Pairs, you can use for arrays and dictionaries.
CollectionService:GetInstanceAddedSignal("Spirit"):Connect(function(instance)
spiritCollect(instance)
end)
for _,instance in ipairs(CollectionService:GetTagged("Spirit")) do
spiritCollect(instance)
end
When I only had the GetInstanceAddedSignal function, some parts were not being recognized by the client touching the part, whereas when I had the pairs loop, they were being recognized by the client. I don’t know why this is happening since all my parts are being spawned in and they aren’t previously created.
The reason you’d want the loop there specifically, is because you may have tagged items beforehand. The InstanceAddedSignal doesn’t trigger if you added the tags in studio, instead of in game.
When you connect an event, whenever the event happens your connected code will run. So you have code which, whenevrer ‘Spirit’ is added, runs spiritCollect.
This connecting happens when the game launches.
But what happens if you add a tag before the game is launched? Well, then the item would have a tag without triggering the event, so it won’t have called spiritCollect.
That’s why you have the ipairs loop. You check ‘Is there any spirit tags already in the game? If so, spiritCollect’