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

Why is my detect script to show GUI isnt running when touching a part?

Asked by 3 years ago

I'm trying to make a script that runs when the specified part is touched and it tells you what difficulty you're in by a GUI coming from the top of the screen but when I test it, I touch the part and then nothing happens. The output doesn't even show a error and I think it's because where the script is placed or something but I don't know the right place for this to work.

local diffReminder = script.Parent:WaitForChild("DifficultyReminder")

local hard = diffReminder.Hard
local med = diffReminder.Medium
local ez = diffReminder.Easy

local debounce = true
local stage = game.Workspace.Stages["2"]
stage.Touched:Connect(function(plr)
    if plr:FindFirstChild("Humanoid") then
        if plr.leaderstats.Stage.Value >2 then
            debounce = false
            ez:Destroy()
        elseif plr.leaderstats.Stage.Value <2 then
            ez:TweenPosition(
                UDim2.new(0.1, 0,0.93, 0),
                "Out",
                "Back",
                0.25,
                false
            )
            wait(4)
            ez:TweenPosition(
                UDim2.new(0.1, 0,-1.93, 0),
                "In",
                "Back",
                0.25,
                false
            )
        end
    end

end)

SCRIPT PARENT:

It's a Local Script Inside of StarterGui

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

If you're trying to get a touch event with a local script, you need to put it in starterplayerscripts and reference the part.

Example:

local part = workspace:FindFirstChild("examplepart")

part.Touched:Connect(function(plr)
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

First thing, don't use a LocalScript for detecting touch and putting it in StarterGui. I would recommend you the simplest method:

-- ServerScript [Put it in the Part]

local debounce = true
local stage = script.Parent

stage.Touched:Connect(function(plr) -- Here plr is not the player, but the part that touched
    print("Touch Detected!")
    if debounce == false then 
        return -- Tell the function not to execute further
    else

    debounce = false -- Puts debounce to false

    local player = game.Players:GetPlayerFromCharacter(plr.Parent) -- It will help you to get the player

    local PlayerGui = player:WaitForChild("PlayerGui") -- It will access the PlayerGui [The contents of StartGui gets copied here]

    local diffReminder = PlayerGui:WaitForChild("DifficultyReminder") -- Change the location to your stuff

    local hard = diffReminder.Hard
    local med = diffReminder.Medium
    local ez = diffReminder.Easy

    if plr:FindFirstChild("Humanoid") then
        if player:WaitForChild("leaderstats").Stage.Value >2 then
            debounce = false
            ez:Destroy()
        elseif player:WaitForChild("leaderstats").Stage.Value <2 then
            ez:TweenPosition(
                UDim2.new(0.1, 0,0.93, 0),
                "Out",
                "Back",
                0.25,
                false
            )
            wait(4)
            ez:TweenPosition(
                UDim2.new(0.1, 0,-1.93, 0),
                "In",
                "Back",
                0.25,
                false
            )
        end
    end

    wait(3) -- Change the time according to your choice
    debounce = true

end)

-- Make sure to add full working debounce and not just the boolean value

Lemme know if it helps!

0
So I tested your method, I put it the code in a serverscript and i put it inside the part. ZombieApocalypz3 35 — 3y
0
When I played the game I still saw no errors in the output but also when i touched it, nothing happened oof. Hope you can try and see why and thanks for helping ZombieApocalypz3 35 — 3y
0
I did some edits in the script and also added a print to check whether it is detecting or not BestCreativeBoy 1395 — 3y

Answer this question