You can write your topic however you want, but you need to answer these questions:
The thing I want to achieve is when a player touches the treadmill’s conveyor, that the conveyor goes the same speed as the humanoid.
But the issue with this is, that only one conveyor activates and not all six.
I’ve tried to ask people for help on several social Servers, but none of them really helped.
local Players = game:GetService('Players')
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')
local Debounce = false
for _, v in pairs(workspace.Treadmills:GetChildren()) do
local TouchConveyor = v:FindFirstChild("Conveyor")
TouchConveyor.Touched:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
if Humanoid then
if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
local Gain = v:FindFirstChild("Gain").Value
if Debounce == true then return end
Debounce = true
Humanoid.WalkSpeed += Gain
task.wait(1)
Debounce = false
print(Player.Name.." ".."His WalkSpeed is".. " "..Humanoid.WalkSpeed)
end
end
end)
while true do
TouchConveyor.Velocity = TouchConveyor.CFrame.lookVector * Humanoid.WalkSpeed
task.wait()
end
end```
I don’t really know how to explain how it works, but you need to put the while loop inside of a spawn function. I think it’s because the loop goes on forever and it waits for the loop to end, until it goes onto the next treadmill. But the problem is, the loop never ends. I think you can use coroutines too, just put the while loop inside the coroutine. I recommend to use coroutine, instead of spawn.
Coroutine (wrap) example:
coroutine.wrap(function()
-- While loop here
end)()
Spawn example:
spawn(function()
-- While loop here
end)
Fixed Script (coroutine):
local Players = game:GetService('Players')
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')
local Debounce = false
for _, v in pairs(workspace.Treadmills:GetChildren()) do
local TouchConveyor = v:FindFirstChild("Conveyor")
TouchConveyor.Touched:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
if Humanoid then
if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
local Gain = v:FindFirstChild("Gain").Value
if Debounce == true then return end
Debounce = true
Humanoid.WalkSpeed += Gain
task.wait(1)
Debounce = false
print(Player.Name.." ".."His WalkSpeed is".. " "..Humanoid.WalkSpeed)
end
end
end)
coroutine.wrap(function()
while true do
TouchConveyor.Velocity = TouchConveyor.CFrame.lookVector * Humanoid.WalkSpeed
task.wait()
end
end)()
end
Fixed Script (spawn):
local Players = game:GetService('Players')
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')
local Debounce = false
for _, v in pairs(workspace.Treadmills:GetChildren()) do
local TouchConveyor = v:FindFirstChild("Conveyor")
TouchConveyor.Touched:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
if Humanoid then
if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
local Gain = v:FindFirstChild("Gain").Value
if Debounce == true then return end
Debounce = true
Humanoid.WalkSpeed += Gain
task.wait(1)
Debounce = false
print(Player.Name.." ".."His WalkSpeed is".. " "..Humanoid.WalkSpeed)
end
end
end)
spawn(function()
while true do
TouchConveyor.Velocity = TouchConveyor.CFrame.lookVector * Humanoid.WalkSpeed
task.wait()
end
end)
end
local debounce = false
for _, v in pairs(workspace.Treadmills:GetChildren()) do
local TouchConveyor = v:FindFirstChild("Conveyor")
TouchConveyor.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local character = hit.Parent
local humanoid = character:WaitForChild("Humanoid")
local Gain = v:FindFirstChild("Gain").Value
if debounce then
return
end
humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
TouchConveyor.Velocity = TouchConveyor.CFrame.lookVector * humanoid.WalkSpeed
end)
debounce = true
humanoid.WalkSpeed += Gain
task.wait(1)
debounce = false
end
end)
end
I’d recommend using a “GetPropertyChangedSignal” event listener for this instead of a “while true do” loop, the event fires each time the “WalkSpeed” property of the “Humanoid” instance changes, and with it the speed of the conveyer should change to match it (providing the line of code which alters the speed of the conveyer works).
To obtain whether a tredmill is being used, you might want to use TouchConveyor:GetTouchingParts() Instead of Touched.
GetTouchingParts() returns you a table of the parts the conveyor is touching and if the length of the table is larger than 1, it means that It is touching. You just have to put it inside a runservice update or while loop to check whether the part it is touching is a player’s bodypart