I’ve made a few posts today asking some questions and have been helped without question!
My goal here, when my portal is available for the 10 seconds, the player should be able to touch it to move on to a random stage in the map. However, the script that directs the teleportation still works even when CanCollide is turned off, I’m not sure how to get around this other than maybe moving the part to ReplicatedStorage and then back to workspace at the same time the portal is gone.
I’ve tried:
Turning CanCollide to false at the same time the visual portal disappears so that the “Touch” wouldn’t function.
Attempted at moving this part to ReplicatedStorage for 30 seconds, then back to workspace for 10 seconds. It didn’t seem to function correctly.
The code below is what I’m using, (Credit to luya_yobanka for helping.)
local targetsFolder = script.Parent.Parent.Parent.starts
local targets = targetsFolder:GetChildren()
function onTouch(part)
local s = math.random(1,#targets)
part.CFrame = CFrame.new(targets[s].Position + Vector3.new(0, 5, 0))
end
script.Parent.Touched:Connect(onTouch)
If you’d like to go the CanCollide route to make sure the portal doesn’t function you could do a change as simple as this:
local targetsFolder = script.Parent.Parent.Parent.starts
local targets = targetsFolder:GetChildren()
function onTouch(part)
if script.Parent.CanCollide == true then
local s = math.random(1,#targets)
part.CFrame = CFrame.new(targets[s].Position + Vector3.new(0, 5, 0))
end
end
script.Parent.Touched:Connect(onTouch)
There’s also some stuff I’d recommend to avoid errors and a debounce like HonestJar recommended, but I didn’t include them in the edit above. Feel free to ask if you’d like to see those edits as well.
I’m pretty new to LUA, I’m not exactly sure what debouncing is? Rather than do the code for me, could you elaborate a little on it so I could grasp the conecept?
The .Touched event fires regardless of CanCollide.
A debounce is just a way to prevent code from running too often.
How you implement one is up to you. Some possibilities include using a boolean variable in the script, and when you run the function you check whether the variable is true or not. You could also do this using the transparency or the cancollide.
You could also use :Disconnect() to remove the Touched connection, and set the Touched connection back up when the portal is active again.
Basically when using something like an .Touched:Connect() event, the script activates every single frame that any object is touching.
You use a debounce to make sure the script only activates every once in a while instead of every single frame. It helps decrease lag and helps prevent weird issues from a player constantly activating the script.
The most basic debounce looks sorta like this:
local debounce = false
function onTouched()
if debounce == false then --make sure debounce isn't active
debounce = true --set debounce to "active"
--insert code here
wait(0.5)
debounce = false --set debounce to "inactive"
end
end
This prevents the script from activating more than once every 0.5 seonds, which prevents some lag and prevents issues.
I would recommend a different method for your case since you need it to activate much faster. Maybe using a debounce tag? It’s like the above but instead of using a value in the script, we use a value placed into the player:
function onTouched(hit)
if hit.Parent:FindFirstChild("debounce") == nil then --make sure the player doesn't have a debounce tag in them
local debounce = Instance.new("IntValue") --creates a value to use as a debounce tag
debounce.Name = "debounce" --make sure the debounce is named debounce
debounce.Parent = hit.Parent --put debounce tag in the player character
--insert code here
wait(0.5)
debounce:Destroy() --destroy the debounce tag so the player can touch the brick again
end
end
There’s much better methods too, but I gave you the two most basic options as examples.