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

How to create "Use/Activate" function?

Asked by 6 years ago

I'm a little advanced in script, but there is just one bump that stops me. I want to create a function in where a player can pick up items off the ground by using their keyboard. Such as he Presses X to get in the vehicle: http://wiki.roblox.com/index.php?title=Libraries_and_Samples/ImageInputLibrary

1
What exactly do you need help with? I see a few parts to this. I feel like what you are looking for is how to listen for a key event(someone presses a keyboard or controller button)? Or do you need help with giving the player the ability to press the key only when close to an item? Bellyrium 310 — 6y
0
"Or do you need help with giving the player the ability to press the key only when close to an item?" - This Right here, I need that. GeezuzFusion 200 — 6y
0
I attempted to show you a way in answers. Feel free to comment if something is left vague. I'm learning to teach Bellyrium 310 — 6y

1 answer

Log in to vote
1
Answered by
Bellyrium 310 Moderation Voter
6 years ago
Edited 6 years ago

There are many ways you can do this, If you want them to get a prompt to press the key, you will need to add a function that watches the distances between the players and items.

--assuming we have the character and Rootpart of a character as a variable
for i,v in pairs(TableListOfItemsToPickUp) do -- goes through each item on the ground
    if (v.PrimaryPart.Position - RootPart.Position).magnitude < 10 then -- if the distance is less than 10 studs
        TellPlayerToPressButton(character) -- function to show GUI 
        local Holder = instance.new("ObjectValue") -- make a holdervalue 
        Holder.Value = v
        Holder.Name = "CanGrab"
        Holder.Parent = game.Players:GetPlayerFromCharacter(character) -- put it in player, maybe player.CanGrabItems or something for orginization
        spawn(function() repeat wait() until 
            (v.PrimaryPart.Position - RootPart.Position).magnitude > 10 
        end)
    end
end

If you loop that, you "should" have a system that puts a marker in your player when you are close to an item. Now just check the player for anything named "CanGrab" in the player, and use the Value of that to manipulate the item.

I should note, this is a VERY messy and bad code. Just a general example.

Ad

Answer this question