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

Why doesn't this work in the server, but in the studio?

Asked by 7 years ago
--Inventory script created by AllegiantX
local UserInput = game:GetService("UserInputService")
--Variables

local plr = game.Players.LocalPlayer
local chr = plr.Character
local deb = script.Parent.Value.Value
local button = script.Parent

--Functions

function openScreen()
    if deb == false then deb = true
        local sb = game.ReplicatedStorage.inventoryScreen
        local sb2 = sb:Clone()
        sb2.Parent = chr
        sb2.CFrame = chr.Head.CFrame + Vector3.new(6,0,-2)
        plr.PlayerGui.UI.Adornee = sb2
        plr.PlayerGui.UI.container.Visible = true
    end
end

function closeScreen()
    if deb == true then deb = false
        plr.PlayerGui.UI.container:TweenSizeAndPosition(UDim2.new(0,0,0,0), UDim2.new(0.5,0,0.5,0))
        wait(1)
        plr.PlayerGui.UI.container.Visible = false
        plr.PlayerGui.UI.container:TweenSizeAndPosition(UDim2.new(0.96, 0,0.96, 0), UDim2.new(0.02, 0,0.02,0))
        if chr:FindFirstChild("inventoryScreen") then
            chr.inventoryScreen:Destroy()
        end
    end
end

--Events

local keyDeb = false
function KeyPressed(input, gameProcessedOnGUI)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local KeyPress = input.KeyCode
        if keyDeb == false and KeyPress == Enum.KeyCode.E then 
            keyDeb = true
          openScreen()
        elseif keyDeb == true then 
            keyDeb = false
            wait(2)
            closeScreen()
        end
    end
end
UserInput.InputBegan:Connect(KeyPressed)

2 answers

Log in to vote
0
Answered by 7 years ago

Try putting repeat wait() until game.Players.LocalPlayer.Character at the very top.

Ad
Log in to vote
0
Answered by 7 years ago

Many people do the same mistake and I also asked this question but now I know how to fix it. When defining variables it's best to use the WaitForChild() or FindFirstChild() functions because they help define the variables better.

This is a fix for you script:

--Inventory script created by AllegiantX
local UserInput = game:GetService("UserInputService")
--Variables

local plr = game.Players.LocalPlayer
local chr = game.Players.LocalPlayer.Character --(Fixed)
local deb = script.Parent.Value:WaitForChild("Value") --You should also name you values, the system will be confused if you say Value.Value. (Fixed using WaitForChild)
local button = script.Parent

--Functions

function openScreen()
    if deb == false then deb = true
        local sb = game.ReplicatedStorage:WaitForChild("inventoryScreen") -- (Fixed using WaitForChild) 
        local sb2 = sb:Clone()
        sb2.Parent = chr
        sb2.CFrame = chr.Head.CFrame + Vector3.new(6,0,-2)
        plr.PlayerGui.UI.Adornee = sb2
        plr.PlayerGui.UI.container.Visible = true
    end
end

function closeScreen()
    if deb == true then deb = false
        plr.PlayerGui.UI.container:TweenSizeAndPosition(UDim2.new(0,0,0,0), UDim2.new(0.5,0,0.5,0))
        wait(1)
        plr.PlayerGui.UI.container.Visible = false
        plr.PlayerGui.UI.container:TweenSizeAndPosition(UDim2.new(0.96, 0,0.96, 0), UDim2.new(0.02, 0,0.02,0))
        if chr:FindFirstChild("inventoryScreen") then
            chr.inventoryScreen:Destroy()
        end
    end
end

--Events

local keyDeb = false
function KeyPressed(input, gameProcessedOnGUI)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local KeyPress = input.KeyCode
        if keyDeb == false and KeyPress == Enum.KeyCode.E then
            keyDeb = true
          openScreen()
        elseif keyDeb == true then
            keyDeb = false
            wait(2)
            closeScreen()
        end
    end
end
UserInput.InputBegan:Connect(KeyPressed)

This should fix the script, if it still doesn't work in multiplayer/game then comment and I will do a advanced fix

By the way, Accept question if it helped/worked

Answer this question