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

This script in ServerScriptService is working in studio but not in game. What is wrong with it?

Asked by 6 years ago

I changed the the variable UserInterface to :WaitForChild but says infinite yield and just doesnt work, but when i try in roblox studio it works

local DataStoreService = game:GetService("DataStoreService")
local TierDataStore = DataStoreService:GetDataStore('TierDataStore')
local StrengthDataStore = DataStoreService:GetDataStore('StrengthDataStore')
local DefenseDataStore = DataStoreService:GetDataStore('DefenseDataStore')
local CoinsDataStore = DataStoreService:GetDataStore('CoinsDataStore')
local HealthMaxDataStore = DataStoreService:GetDataStore('HealthMaxDataStore')
local ExperienceDataStore = DataStoreService:GetDataStore('ExperienceDataStore')
local ExpNeededDataStore = DataStoreService:GetDataStore('ExpNeededDataStore')
local Stats = game.ReplicatedStorage.Media.Stats:Clone()
local StarterGui = game:GetService('StarterGui')

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        wait(2)
        local UserInterface = player.PlayerGui.UserInterface
        Stats.Parent = player
        Stats.Tier.Value = TierDataStore:GetAsync(player.UserId) or 1
        Stats.Strength.Value  = StrengthDataStore:GetAsync(player.UserId) or 1
        Stats.Defense.Value = DefenseDataStore:GetAsync(player.UserId) or 1
        Stats.Coins.Value = CoinsDataStore:GetAsync(player.UserId) or 100
        Stats.HealthMax.Value = HealthMaxDataStore:GetAsync(player.UserId) or 100
        Stats.Experience.Value = ExperienceDataStore:GetAsync(player.UserId) or 0
        Stats.ExpNeeded.Value = ExpNeededDataStore:GetAsync(player.UserId) or 1000


        Stats.Tier.Changed:Connect(function()
            Stats.Tier.Value = TierDataStore:SetAsync(player.UserId, Stats.Tier.Value)
            print('saved')
        end)
        Stats.Strength.Changed:Connect(function()
            Stats.Strength.Value = StrengthDataStore:SetAsync(player.UserId, Stats.Strength.Value)
            print('saved')
        end)
        Stats.Defense.Changed:Connect(function()
            Stats.Defense.Value = DefenseDataStore:SetAsync(player.UserId, Stats.Defense.Value)
            print('saved')
        end)
        Stats.Coins.Changed:Connect(function()
            Stats.Coins.Value = CoinsDataStore:SetAsync(player.UserId, Stats.Coins.Value)
            print('saved')
        end)
        Stats.HealthMax.Changed:Connect(function()
            Stats.HealthMax.Value = HealthMaxDataStore:SetAsync(player.UserId, Stats.HealthMax.Value)
            print('saved')
        end)
        Stats.Experience.Changed:Connect(function()
            Stats.Experience.Value = ExperienceDataStore:SetAsync(player.UserId, Stats.Experience.Value)
            print('saved')
        end)
        Stats.ExpNeeded.Changed:Connect(function()
            Stats.ExpNeeded.Value = ExpNeededDataStore:SetAsync(player.UserId, Stats.ExpNeeded.Value)
            print('saved')
        end)


        char.Humanoid.Health = Stats.HealthMax.Value
        UserInterface.TierFrame.Tier.Text = 'Tier: '..Stats.Tier.Value
        player.PlayerGui:SetTopbarTransparency(1)
        StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
        StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health , false)
        StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

        UserInterface.TextLabel.Text = 'Coins:'..Stats.Coins.Value
        char.Humanoid.HealthChanged:Connect(function()
            player.PlayerGui.UserInterface.HealthBar.Frame:TweenSize(UDim2.new(char.Humanoid.Health/100, 0, 1, 0),'Out', 'Quad', 0.01, false)
        end)


        local tag = game.ServerStorage.BillboardGui:Clone()
        tag.Parent = char.Head
        tag.TextLabel.Text = player.Name
        if player.Name == 'Sage_Prodigy' then
            tag.TextLabel.Text = 'Owner'
            tag.TextLabel.TextColor3 = Color3.fromRGB(255,237,35)
        end
    end)
end)

1 answer

Log in to vote
1
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago

If your game is FE and the gui is in StarterGui, then once cloned to PlayerGui, it becomes a local gui, meaning the server cannot see it, only the player can.

So what you need to do is either use RemoteEvents to tell the client to change something in that gui (which will probably take a while to script) OR place the gui i.e. in ServerStorage and clone it to the player's PlayerGui on PlayerAdded. That way it becomes a non-local gui which you can access from the server.

local UserInterface = game.ServerStorage.UserInterface

game.Players.PlayerAdded:Connect(function(plr)
    UserInterface:Clone().Parent = plr:WaitForChild("PlayerGui")
    --if the gui gets removed when the player respawns, connect this CharacterAdded event aswell:
    plr.CharacterAdded:Connect(function()
        UserInterface:Clone().Parent = plr:WaitForChild("PlayerGui")
    end)
end)
Ad

Answer this question