Creating a client-sided collection service script?

I have this collection service script which I have applied to parts which change the players movement, then disable for a few seconds. I want to make this feature client-sided as it is currently server sided, so it leaves room for annoyance when trying play the game. I’m relatively new to scripting and would appreciate any help that would guide me towards making the part disable for only the client when it is touched, so that other users can also use the part whenever they would like instead of waiting for it to respawn.

So far I have tried changing the script to a local script as well as putting the script into StarterPlayer and StarterCharacter but I haven’t had too much luck, same with trying to look up solutions online.

Here is the script.

local CollectService = game:GetService("CollectionService")
local TaggedParts = CollectService:GetTagged("SpeedTag")
local color = Color3.new(0, 0, 0)
local color2 = Color3.new(1, 0, 0)
local color3 = ColorSequence.new{
	ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 0, 0)),
	ColorSequenceKeypoint.new(.5, Color3.fromRGB(255, 107, 107)),
	ColorSequenceKeypoint.new(1, Color3.fromRGB(255, 255, 255))
}
local lifetime = NumberRange.new(0.5,1)
local lifetime2 = NumberRange.new(.3,.3)
local players = game:GetService("Players")


for _,TaggedPart in pairs(TaggedParts) do
	local highlight = Instance.new("Highlight")
	highlight.Parent = TaggedPart
	highlight.FillTransparency = 1
	highlight.OutlineTransparency = .5
	highlight.DepthMode = "Occluded"
	function onTouched(hit)
		local character = hit.Parent
		local root = character:FindFirstChild("HumanoidRootPart")
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid then 
			local effect = Instance.new("ParticleEmitter")
			effect.Parent = root
			effect.Color = color3
			effect.Lifetime = lifetime
			TaggedPart.CanTouch = false
			TaggedPart.CanQuery = false
			local sound = Instance.new("Sound")
			sound.RollOffMaxDistance = 100
			sound.SoundId = "rbxassetid://99173388"
			sound.Parent = TaggedPart
			sound.Volume = .5
			sound:Play()
			TaggedPart.Color = color
			TaggedPart.Transparency = .5
			hit.Parent.Humanoid.WalkSpeed = 50
			wait(1)
			hit.Parent.Humanoid.WalkSpeed = 45
			wait(.3)
			hit.Parent.Humanoid.WalkSpeed = 40
			wait(.4)
			hit.Parent.Humanoid.WalkSpeed = 35
			wait(.4)
			hit.Parent.Humanoid.WalkSpeed = 30
			wait(.5)
			hit.Parent.Humanoid.WalkSpeed = 25
			wait(.5)
			hit.Parent.Humanoid.WalkSpeed = 20
			sound:Destroy()
			effect:Destroy()
			wait(3)
			TaggedPart.CanTouch = true
			TaggedPart.CanQuery = true
			TaggedPart.Color = color2
			TaggedPart.Transparency = 0
		end
	end

	TaggedPart.Touched:Connect(onTouched)
end

this script looks like it should work on the client. the reason you might be having problems though might be because the parts aren’t loading in quick enough. so when you do

local TaggedParts = CollectService:GetTagged("SpeedTag")

it probably returns an empty table as none of the parts have actually finished loading.
the reason this isnt an issue with a server script is because when server scripts run most of the time everything will already be loaded in however for the client its different.
to fix this you would need to make a line that waits for all parts that have this tag to be loaded in. or if your game has some sort of loading screen you could run this script when that’s finished or something like that.

1 Like

This worked! Thanks for helping me out ^^

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