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

How do I properly make this anchor script use click detector?

What I am trying to do: I want this script to anchor the player when they click the part but nothing happens. Nothing in output what went wrong?

local AnchorDuration = 2.4            -- how long the player stays anchored
local CooldownTime = 158             -- how long until the player can be anchored again
local CooldownDistance = 10         -- how far away the player needs to walk before they can be anchored again
local UseCooldownTime = true        -- whether or not you want to use a cooldown time
local UseMagnitudeCooldown = true   -- whether or not you want to use a magnitude cooldown

local PlayerUniqueDebounce = {}     -- player unique debounces are good for things like this

function onClicked(Object)
    if not game:GetService('Players'):GetPlayerFromCharacter(Object.Parent) or not Object.Parent:FindFirstChild('HumanoidRootPart') then
        print('Works (' .. Object:GetFullName() .. ').')
        return 
    end
    if PlayerUniqueDebounce[Object.Parent.Name] then
        print(Object.Parent.Name .. ' is debouncing.')
        return -- this ends the function if the player is debouncing
    end
    PlayerUniqueDebounce[Object.Parent.Name] = true
    Object.Parent.HumanoidRootPart.Anchored = true
    wait(AnchorDuration)
    Object.Parent.HumanoidRootPart.Anchored = false
    if UseCooldownTime then
        wait(CooldownTime)
    end
    if UseMagnitudeCooldown then
        repeat
            wait(1)
        until (Object.Parent.HumanoidRootPart.Position - script.Parent.Position).magnitude >= CooldownDistance
    end
    PlayerUniqueDebounce[Object.Parent.Name] = nil
end
script.Parent.MouseClick:Connect(onClicked) --click detector

1 answer

✓ Accepted answer

This is how I did it.

Most of that stuff is not really necessary.

local anchorTime = 2.4 --// How long the Player is anchored for
local coolDownTime = 158 --// How long until they can click it again and be anchored
local debounce = true --// This is to stop spam anchoring

script.Parent.ClickDetector.MouseClick:Connect(function(hit) --// Get the Player from clicking the button
	if game:GetService("Players"):FindFirstChild(hit.Name) then --// If the Player exists
		local player = game:GetService("Players"):FindFirstChild(hit.Name) --// Set a variable equal to the Player
		if debounce == true then --// When it's not on cooldown
			debounce = false --// Stops spam here
			local char = player.Character or player.CharacterAdded:Wait() --// Gets the Player's character
			if char then --// If Characte exists
				char:FindFirstChild("HumanoidRootPart").Anchored = true --// Anchors HRP, all characters have this
				wait(anchorTime) --// Waits for the anchor time to unanchor
				char:FindFirstChild("HumanoidRootPart").Anchored = false --// Unanchors the HRP
				wait(coolDownTime) --// Waits for the cooldown time and then sets debounce to true so they can click it again
				debounce = true --// Sets the value to true so they can click it again
			end
		end
	end
end)

How the part looks like: https://gyazo.com/8bbfcab67b03cea82fdb77a5aab4dd88

Everything I did is commented next to every line and it explains how it works and what I did.

Most things in the script you wrote are not necessarily required, but if you want you can incorporate the distance if you feel like it but I don't really think it's required because you can just set the distance of the click detector to be 10 so they have to be 10 studs close to click the button.

Anyways, hope this works. If this doesn't work, let me know so I can fix it for you.

4 comments

0

Ty very much

0

No problem!

0

Could you help me with another question in on my profile titled 'Can't find script that's causing error in the output, help?'

0

Sure!

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