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

Sound Binding (?)

Asked by 10 years ago

The trouble still stands. I've been trying for the past month or so now to see if I can get sound bound to a key on a computer ie:

    *If key = "V"
        game.Workspace.Sound:Play()
    end*

It does not only not work, but plays over a large distance. I've tried for over a month, and it covers the map when I only wanted a set distance, no-one's been a great help so far.

3 answers

Log in to vote
1
Answered by 10 years ago

This script will add the sound to every user within a distance and then plays it. After 5 seconds the sound is again removed from each player. Put this in a localscript and on every respawn clone the localscript to the players character. I believe that if a sound is put in the PlayerGui, it will only play locally.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local sound = game.Workspace.Sound
local soundLength = 5
local maxDist = 30
local sounds = {}

mouse.KeyDown:connect(function(key)
    if key == "v" then
        for _,v in pairs(game.Players:GetChildren()) do
            local dist = (v.Character.Torso.Position - player.Character.Torso.Position).magnitude
            if dist <= maxDist then
                local c = sound:Clone()
                table.insert(sounds, c)
                c.Parent = v.PlayerGui
                c:Play()
            end
        end
        wait(soundLength)
        for _,v in pairs(sounds) do
            v:Destroy()
        end
    end
end)

This script may not be failsafe. You'll have to adjust it to what you need.

Ad
Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

You have to think about what you have access to.

In ROBLOX, in order to grab user input and do something with it, you have to use a LocalScript. For the purposes of this tutorial, I will assume you're not using Filtering.

The way I prefer to handle user input is the UserInputService. This service contains a multitude of events for input events, including those exclusive to Touch devices.

One of these events is called InputBegan. Is is equivalent to KeyDown, but has unique 'keycodes' for every key. You are given a singular "UserInputObject" when you connect all events with UserInputService, so some sanity checking is required to access anything safely:

local uis = Game:GetService("UserInputService")
local sound = Workspace.Part1.Sound --In order to make a sound reduce volume at a distance, it has to be the child of some part. If you want this to play at the Player's location, you'll have to parent it to one of their Body parts.

uis.InputBegan:connect(function(inst)
    if inst.UserInputType = Enum.UserInputType.Keyboard then
        if inst.KeyCode = Enum.KeyCode.V then
            sound:Play()
        end
    end
end)

Log in to vote
-3
Answered by
Nidoxs 190
10 years ago

I think i you put the sound in a brick and make the brick small and far away from the 'Object, person' you can't hear it. Set the volume to 0.6 and I think that will work! :) Sorry might not be right I'm a dumbo! Worked for me!

Answer this question