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

Weapon detector doesn't play the sound, when I walk through it with a tool named as a weapon. Help?

Asked by
ZenTGE 4
6 years ago
Edited by Shawnyg 6 years ago
Sound = game.Workspace.scanner.Sound
names = "StA-45, Basic 9mm"
starterpack = game.StarterPack
script.Parent.Touched:Connect(function(hit)
    if names.Parent == starterpack then
        script.Parent:WaitForChild("Sound")
        Sound:Play()
        wait(2)
        Sound:Stop()
    end
end)

I have a tool in StarterPack named StA-45. When I walk through the detector, it doesn't play my alarm sound. I have the ID in and everything. No output errors. Any help?

0
Edit: Put code in a code block Shawnyg 4330 — 6y

1 answer

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Hm, so you've given it your best so I'll help you out here. Looking at your variable names, I'm assuming you're going to want to put multiple weapon names in there, so for that you're going to need a table. To go through all of the items in this table, for this task, I'm going to be using the generic for loop (Wiki). After reading up on those, you'll find out the generic for loop is used to go through each index (value/place/item) of a table.

Once you have a for loop setup, I'm going to place it inside a isNotAllowed function to have the script check if the tool name is on the prohibited list. Assuming you're going to want to check the entire player's inventory, you'll have to loop through their backpack as well. Now onto the code: (I've commented it to help you better understand it)

local sound = game.Workspace.scanner.Sound
local prohibitedItems = {"StA-45", "Basic 9mm"} -- Assuming these are 2 separate weapons

function isProhibited(name) -- Takes the name parameter
    local is = false -- Value that will be returned
    for _,v in pairs(prohibitedItems) do
        if v:lower() == name:lower() then -- :lower() makes the string lowercase. I used it in case the capitalization varied. Remove if you wish
            is = true -- the name is a match against the prohibitedItems list. hence the if statement
        end -- Close the if statement
    end -- Close the for loop
    return is -- Return the value true/false.
end -- Close the function

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Method used to return a player from their charater
    if player then -- Checks to see if whatever touched it belongs to a Player's Character
        for _,item in pairs(player:WaitForChild('Backpack'):GetChildren()) do
            if isProhibited(item.Name) then -- Calls the function on the item's name
                sound:Play()
                wait(2)
                sound:Stop()
                break -- Having it end the for loop early since it already found one bad object
            end -- Close the if statement
        end -- Close the for loop
    end -- Close the first if statement
end) -- Close the connection line/function
0
Note: I didn't test this code, but it should work as expected Shawnyg 4330 — 6y
Ad

Answer this question