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

Change something when players are in game? (Specific players)

Asked by 7 years ago

Ok so heres the deal: I have this script and I want to to change the decal ID when these 2 certain players are in game, then change when they leave. Problem is,if they both join and if one leaves, it reverts, and the other one can still be in game and see it. Please help?

--Current script, "Mya" is name of the joke i have with friends, so do ignore
local PreviousDecal = game.Workspace.RandomThing:FindFirstChild("Decal")
if PreviousDecal then PreviousDecal:Destroy() end -- So theres only 1

local Mya = Instance.new("Decal")
Mya.Parent = game.Workspace.RandomThing
Mya.Texture = DECAL ID

game.Players.PlayerAdded:connect(function(p)
    if p.UserId == 17408797 or 3976483464 then
    local Mya = game.Workspace.RandomThing:FindFirstChild("Decal")
    Mya.Texture = IDFORCOVERUP
    end
end)


game.Players.PlayerRemoving:connect(function(p)
    if p.UserId == 17408797 or 3976483464 then
    local Mya = game.Workspace.RandomThing:FindFirstChild("Decal")
    Mya.Texture = DECAL ID
    end
end)

Any help appreicated.

3 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

You aren't checking if the other player is in the game also. Just simply add a for loop to scroll through all the players. If even one of the IDs match up, the for loop will return and not run the rest of the code. If it doesn't find any of them, the decal will remove.

Code:

local PreviousDecal = game.Workspace.RandomThing:FindFirstChild("Decal")
if PreviousDecal then PreviousDecal:Destroy() end -- So theres only 1

local Mya = Instance.new("Decal")
Mya.Parent = game.Workspace.RandomThing
Mya.Texture = DECAL ID

game.Players.PlayerAdded:connect(function(p)
    if p.UserId == 17408797 or 3976483464 then
    local Mya = game.Workspace.RandomThing:FindFirstChild("Decal")
    Mya.Texture = IDFORCOVERUP
    end
end)


game.Players.PlayerRemoving:connect(function()
    for _, p in pairs(game.Players:GetChildren()) do
        if p.UserId == 17408797 or 3976483464 then
            return true
        end
    end
    local Mya = game.Workspace.RandomThing:FindFirstChild("Decal")
    Mya.Texture = DECAL ID
end)
0
If you know the other players name, you can just use findfirst child Volodymyr2004 293 — 7y
0
that will not work Etheroit 178 — 7y
0
he / she wanted script when both ppl are in the game Etheroit 178 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

You need a boolean value to check if they're both in game, and if they're both not in game. Below replace 'UserId == 0' with the IDs. Here's what you need to add:

p1 = false
p2 = false

game.Players.PlayerRemoving:connect(function(p)
    if p.UserId == 0 then
        p1 = true
        if p1 and p2 then
            -- Decal code
        end
    elseif p.UserId == 0 then
        p2 = true
        if p1 and p2 then
            -- Decal code
        end
    end
end)

game.Players.PlayerAdded:connect(function(p)
    if p.UserId == 0 then
        p1 = false
        if p1 == false and p2  == false then
            -- Remove Decal Code
        end
    elseif p.UserId == 0 then
        p2 = false
        if p1 == false and p2 == false then
            -- Remove Decal Code
        end
    end
end)
0
uh that wont work like he wanted to. currently your script needs only one player to join (both p1 and p2 are false) so only one need to join to make dis... Etheroit 178 — 7y
0
p1 and p2 should be true at the beginning Etheroit 178 — 7y
Log in to vote
-1
Answered by
Etheroit 178
7 years ago
Edited 7 years ago
local PreviousDecal = game.Workspace.RandomThing:FindFirstChild("Decal")
if PreviousDecal then PreviousDecal:Destroy() end

local Mya = Instance.new("Decal")
Mya.Parent = game.Workspace.RandomThing
Mya.Texture = DECAL ID -- set the decal id (before ppl join)

local plr1 = false
local plr2 = false

function Reload()
 if plr1 and plr2 then
 Mya.Texture = SHOWED DECAL ID -- set the decal id ( after join )
 else
 Mya.Texture = DECAL Id -- set the decal id after they leave
end

game.Players.PlayerAdded:connect(function(p)
 if p.UserId == 17408797 then -- id of first user
 plr1 = true
 elseif p.UserId == 3976483464 then -- id of second user
 plr2 = true
 end
 Reload()
end)


game.Players.PlayerRemoving:connect(function(p)
    if p.UserId == 17408797 then -- first id
 plr1 = false
 elseif p.UserId ==3976483464 then -- second id
 plr2 = false
 end
 Reload()
end)

This should work EDIT: Explanation of not working thing especially for kingdom5. If You want to check are both players in game then I prefer adding two bolean variables and when some guy join change one of them to true (after that check are both variables set to true, if yes, set decal texture, if no, revert) You did script which didnt check both players. You just checked is one guy in game (note: If two people joined and one left then it didnt work too cause You reverted decam. So if You want to make this avaible when only one of these guys is in then just replace and by or in Reload function)

0
You have not explained the problem. User#5423 17 — 7y

Answer this question