Hi. I am making a platformer game and created an orb that boosts the player vertically on touch. Here is my script.
local RunService = game:GetService("RunService")
local cooldown = false
function onTouched(part)
local h = part.Parent:findFirstChild("Humanoid")
if h ~= nil then
if cooldown == false then
RunService.Heartbeat:Wait()
h.Parent.PrimaryPart.Velocity = Vector3.new(0, 80, 0)
script.Parent.Boost:Play()
cooldown = true
wait(.3)
cooldown = false
else
end
end
end
script.Parent.Touched:connect(onTouched)
This is placed inside of a part. It works, however since it’s server sided players with poorer connections experience a delay before being boosted or are boosted far higher than intended. I am not sure how to make this local. Any help would be appreciated!
If you want to handle the touched events locally then you could make a folder with all the boost orbs, and then put something like this in a LocalScript inside of StarterPlayerScripts:
local RunService = game:GetService("RunService")
local Orbs = workspace:WaitForChild'Orbs' -- Wait for the folder that has all the boost Orbs
local CoolDown = false
local function OnTouched(Orb, Touched)
local Humanoid = Touched.Parent:findFirstChild("Humanoid")
if Humanoid ~= nil then
if CoolDown == false then
Humanoid.Parent.PrimaryPart.Velocity = Vector3.new(0, 80, 0)
Orb.Boost:Play()
CoolDown = true
wait(0.3)
CoolDown = false
end
end
end
-- Connect the .Touched event to all the boost Orbs in the orb folder
for i, Orb in pairs(Orbs:GetChildren()) do
Orb.Touched:Connect(function(Part)
OnTouched(Orb, Part)
end)
end