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

how do i make a giver that works with click detector?

Asked by 5 years ago

yo i managed to make a giver but now im trying to make it work on click so do you know why this script doesnt work?

local Giver = script.Parent
local Gear = script.Parent.Handgun
local idk = false
function give(Part)
    local hum = Part.Parent:findFirstChild("Humanoid")
    if (hum ~= nil) and idk == false then
        idk = true 
        local Location =
        game:GetService("Players"):GetPlayerFromCharacter(hum.Parent)
        local New = Gear:Clone()
        New.Parent = Location.Backpack
        Giver.BrickColor = BrickColor.new("Really red")
        wait(3)
        Giver.BrickColor = BrickColor.new("Medium stone grey")
        idk = false
    end
end
Giver.ClickDetector.MouseClick:Connect(give)

2 answers

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

You don't put tools in the player. You can either put it in the character or put it in the player's backpack. Here's the fixed script.

local Location = game:GetService("Players"):GetPlayerFromCharacter(hum.Parent).Backpack

Also, FindFirstChild may need to be capitalized, I dunno.

0
doesnt work TFlanigan 86 — 5y
0
also lmao u just copied those 2 lines from the script TFlanigan 86 — 5y
0
I forgot to edit them joshthegamer456 93 — 5y
0
fixed. joshthegamer456 93 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

It's because you don't understand what MouseClick passes as an argument. MouseClick doesn't pass a part; it passes the player who clicked the ClickDetector.

Edited script:

local Giver = script.Parent
local Gear = script.Parent.Handgun
local idk = false
function give(Player)
    if Player then
        if not idk then
             idk = true 
             local New = Gear:Clone()
             New.Parent = Player.Backpack
             Giver.BrickColor = BrickColor.new("Really red")
             wait(3)
             Giver.BrickColor = BrickColor.new("Medium stone grey")
             idk = false
        end
    end
end
Giver.ClickDetector.MouseClick:Connect(give)

Only Touched passes a BasePart as an argument. A ClickDetector's MouseClick event always passes the player who clicked on the ClickDetector.

Answer this question