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 access the player from a mousebutton1 down event?

I've made this script and I want it to clone an item into the player's inventory if they don't have it already, and I don't know all of the problems but I don't think I got to the player right

local getPlayerFromCharacter
script.Parent.MouseButton1Down:Connect(function(hit)
	script.Parent.BackgroundTransparency = 0.9
local player = getPlayerFromCharacter(hit.Parent)
local arizona = game.ReplicatedStorage:WaitForChild("??????? ???")
if player.Backpack:FindFirstChild("??????? ???") then
	print("already has")
	script.Parent.BackgroundTransparency = 0
	script.Parent.TextBox.Visible = true
	wait(1)
	script.Parent.TextBox.Visible = false
elseif not player.Backpack:FindFirstChild("??????? ???") then
	local clone = arizona:Clone()
	clone.Parent = player.Backpack
	script.Parent.BackgroundTransparency = 0
	end

end)

2 comments

0

you cant use hit because thats only for the touched event. you can put the part that detects the player in a local script and use remotes to connect the rest of the script.

0

so i can only use remote events to access the player from the script? :(

1 answer

✓ Accepted answer

###The parameters to the MouseButton1Down event are the X and Y positions of the mouse, so you're trying to get the parent of a number, which numbers don't have. Since you used a LocalScript, you can just use LocalPlayer. Also, MouseButton1Down runs code whilst the mouse is down on the GuiButton, which can repeat code. You should use MouseButton1Click, which only runs code on the click. Unless the behaviour wanted is the code repeating. The MouseButton1Click event passes no parameters.

local Players = game:GetService("Players")
local player = Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    script.Parent.BackgroundTransparency = 0.9
    local arizona = game.ReplicatedStorage["??????? ???"]

    if player.Backpack:FindFirstChild(arizona.Name) then
        print("already has")
        script.Parent.BackgroundTransparency = 0
        script.Parent.TextBox.Visible = true
        wait(1)
        script.Parent.TextBox.Visible = false
    else
        local clone = arizona:Clone()
        clone.Parent = player.Backpack
        script.Parent.BackgroundTransparency = 0
    end
end)

3 comments

0

I wasn't using a local script :( I put this script on a surface gui but I think i can use this for another side of a remote event right?

0

Oh you should have put the SurfaceGui in StarterGui and set the Adornee to the part

0

ok i will do that thank you

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