I want to create a system where players run through gates and get awarded with coins etc. I am currently using CollectionService as I have 500 gates.
In my game players travel at high walk speeds at over 10k+. When a speed of about 2k or over is reached the touched event on the level gates sometimes doesn’t register.
This is my script:
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("events")
local gatePassed = events:WaitForChild("gatePassed")
local function onHit(hit,tagged)
if hit.Parent:FindFirstChild("Humanoid")then
local character = hit.Parent
local player = Players:GetPlayerFromCharacter(character)
if player then
local gateNumber = tonumber(tagged.SurfaceGui.TextLabel.Text)
if gateNumber == 500 then
character.HumanoidRootPart.Velocity = Vector3.new(0, 0, 0)
end
local leaderstats = player:FindFirstChild("leaderstats")
local coins = leaderstats:FindFirstChild("Coins")
if gateNumber < 16 then
coins.Value = coins.Value + 1
elseif gateNumber >= 16 and gateNumber < 46 then
coins.Value = coins.Value + 3
elseif gateNumber >= 46 and gateNumber < 106 then
coins.Value = coins.Value + 5
elseif gateNumber >= 106 and gateNumber < 226 then
coins.Value = coins.Value + 10
elseif gateNumber >= 226 then
coins.Value = coins.Value + 15
end
gatePassed:FireClient(player)
tagged:Destroy()
end
end
end
local function onInstanceAdded(instance)
instance.Touched:Connect(function(hit)
onHit(hit,instance)
end)
end
local taggedGate = CollectionService:GetTagged('winGate')
for _, tagged in pairs(taggedGate) do
onInstanceAdded(tagged)
end
CollectionService:GetInstanceAddedSignal('winGate'):Connect(onInstanceAdded)
After researching this I have seen raycasting to be a possible solution but I am unsure how I would make this work with 500 parts. Any suggestions would be appreciated.