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

Equip katana, press "v" to turn invisible?

Asked by 5 years ago

Hey so I'm making a katana for a ninja and I want it to be stealthy. I want it so when you equip the Katana, and press 'v', you turn invisible for 10 seconds

I already tried, but the code doesn't work. Can someone help me out?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local function onKeyDown(key)
if key == "v" then
    print("v")
    local c = script.Parent.Parent:GetChildren(c)
        if c:IsA('Part') then
            c.Transparency = 1
        wait(10)
            c.Transparency = 0
        end
    end
end

1 answer

Log in to vote
0
Answered by
Rheines 661 Moderation Voter
5 years ago
Edited 5 years ago

To change your invisibility you'd need to use RemoteEvents for it to be seen by all clients. Also, you need to use a for loop in the server so that it will update all the parts of the player.

LocalScript:

--Add a new remote event in ReplicatedStorage.
local ReplicatedStorage = game.GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("RemoteEvent")

--This is the portion of your script, use UserInputService instead of GetMouse() next time but it is not the point of this discussion currently.


local function onKeyDown(key)
if key == "v" then
    --Fire the remote event to the server.
    Remote:FireServer()
end


Server Script:

--ReplicatedStorage and the RemoteEvent.
local ReplicatedStorage = game.GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("RemoteEvent")

--Listen for connections from the client. 'player' is a default argument.
Remote.OnServerEvent:Connect(function(player)
    --Get the player's character.
    local body = player:WaitForChild("Character"):GetChildren()

    --This is a generic for loop that you did wrong.
    --Turn all invisible.
    for _,v in pairs(body) do
        if v:IsA("BasePart") then
            v.Transparency = 1
        end
    end
    wait(10)
    --Turn back to visible.
    for _,v in pairs(body) do
        if v:IsA("BasePart") then
            v.Transparency = 0
        end
    end
end)

If it does not work, please let me know as I did not test it beforehand.

Ad

Answer this question