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

How to make clicking a part give a gear?

Door = script.Parent
cooldown = 5 --How much time in seconds it takes for the door to open again. change if you wish.
value = false

function OnClicked()
	if (value == false) then
		value = true
	end
   local cloned = game.Lighting.Water:Clone()
    game.Lighting.Water:Clone().Parent = game.Players.LocalPlayer.Backpack


    Door.ClickDetector.MaxActivationDistance = 0
	wait(cooldown)
	
	value = false
    Door.ClickDetector.MaxActivationDistance = 10
end

script.Parent.ClickDetector.MouseClick:connect(OnClicked)

I try clicking on Door to get the 'Water' gear but it says LocalPlayer is a nil value. How can I tell it to give the gear to whoever is clicking?

It works in the studio, but not in the real game.

Door = script.Parent
cooldown = 5 --How much time in seconds it takes for the door to open again. change if you wish.
value = false

function OnClicked(player)
	if (value == false) then
		value = true
	end
   local cloned = game.Lighting.Water:Clone()
    game.Lighting.Water:Clone().Parent = player

    cloned.Handle.Anchored = false
    cloned.Handle.Locked = false

	Door.CanCollide = false
    Door.ClickDetector.MaxActivationDistance = 0
	wait(cooldown)
	
	value = false
    Door.ClickDetector.MaxActivationDistance = 10
end

script.Parent.ClickDetector.MouseClick:connect(OnClicked)

2 comments

0

Add the argument ("player") at the start of your function it should work then. For example function OnClicked() would become function OnClicked(player) also change the clone parent to "player"

0

Okay, I put up the revised script. This time it doesn't give me the tool on studio either. And output doesn't show me what's wrong.

1 answer

Door = script.Parent
cooldown = 5 --How much time in seconds it takes for the door to open again. change if you wish.
value = false

function OnClicked(player)
    if (value == false) then
        value = true
    end
   local cloned = game.Lighting.Water:Clone()
    game.Lighting.Water:Clone().Parent = player.Backpack -- Notice this

    cloned.Handle.Anchored = false
    cloned.Handle.Locked = false

    Door.CanCollide = false
    Door.ClickDetector.MaxActivationDistance = 0
    wait(cooldown)

    value = false
    Door.ClickDetector.MaxActivationDistance = 10
end

script.Parent.ClickDetector.MouseClick:connect(OnClicked)

You were parenting your tool to the player itself rather than the player's Backpack. Hopefully your script works now.

Best regards!

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