Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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)

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!

3 comments

0

So I tested your method, I put it the code in a serverscript and i put it inside the part.

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

0

I did some edits in the script and also added a print to check whether it is detecting or not

This discussion is preserved as it was. New answers, comments, and votes are closed.