Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I clone a script from touching a part when 2 players touch it at the same time?

Asked by 3 years ago

Hi, so I want to make a part that when you touch it it clones a localscript into that player's playergui. Problem is, when 2 players touch it at the same time, the script attempts to clone the localscript into BOTH players' playergui, which interferes with the script. Duplicate LocalScripts in one PlayerGui cause the script to break. How do I make it so the script only puts the localscript into players' playerguis when they haven't touched it previously?

local Players = game:GetService("Players")
local cutscene=script.LocalScript
local cutsceneinsert=cutscene:Clone()
local part = script.Parent
local function onTouched(part)
    local player = Players:GetPlayerFromCharacter(part.Parent)
    if not player then return end
    cutsceneinsert.Parent=player.PlayerGui
    part.Parent:FindFirstChild("Humanoid").JumpPower = 0
    part.Parent:FindFirstChild("Humanoid").WalkSpeed = 0
    wait(13)
    part.Parent:FindFirstChild("Humanoid").JumpPower = 50
    part.Parent:FindFirstChild("Humanoid").WalkSpeed = 20
end
part.Touched:Connect(onTouched)
0
Add a debounce in the script so that it waits after the first person touches it. TheOnlySmarts 233 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

If you want to make it so the player does not get more than one copy of the local script, you would have to look through his PlayerGui and check if it actually contains the local script. If it doesn't then you can add it.

If you don't want the rest of the code from running, simply return if it finds the local script in the PlayerGui.

local Players = game:GetService("Players")
local cutscene=script.LocalScript
local cutsceneinsert=cutscene:Clone()
local part = script.Parent
local function onTouched(part)
    local player = Players:GetPlayerFromCharacter(part.Parent)
    if not player or player.PlayerGui:FindFirstChild(cutscene.Name)then return end
    cutsceneinsert.Parent=player.PlayerGui
    part.Parent:FindFirstChild("Humanoid").JumpPower = 0
    part.Parent:FindFirstChild("Humanoid").WalkSpeed = 0
    wait(13)
    part.Parent:FindFirstChild("Humanoid").JumpPower = 50
    part.Parent:FindFirstChild("Humanoid").WalkSpeed = 20
end
part.Touched:Connect(onTouched)
Ad

Answer this question