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

Values created by datastore not being detected by localscript? [Figured it out on my own]

Asked by 9 years ago

I'm working on a player search system which involves a datastore to store the usernames of each player.

This is my datastore script [server-side]:

local datastore = game:GetService("DataStoreService"):GetDataStore("rP")
print("Got datastore!")
local serverstorage = game:GetService("ServerStorage")
print("Got serverstorage!")
game.Players.PlayerAdded:connect(function(plyr)
    local rp = Instance.new("IntValue")
    rp.Parent = serverstorage
    rp.Name = "rP-"..plyr.Name
    print("rp made!")
    local key = "plyr"..plyr.userId
    print("Key generated!")
    local username = Instance.new("StringValue")
    username.Parent = serverstorage
    username.Name = plyr.Name
    username.Value = plyr.Name
    print("Username has been created and inserted into serverstorage.")

    local userid = Instance.new("IntValue")
    userid.Parent = serverstorage
    userid.Name = plyr.Name.."-id"
    userid.Value = plyr.userId
    print("userid acquired!")

    local savedvalue = datastore:GetAsync(key)
    print("Savedvalue loaded")

    if savedvalue then
        savedvalue[1] = rp.Value
        savedvalue[2] = username.Value
        savedvalue[3] = userid.Value
        print("Savedvalue table generated")
    else
        datastore:SetAsync(key, {rp.Value, username.Value,userid.Value})
        print("Items saved")
    end
end)

And my search script [client-side; local script]:

local frame = script.Parent.Parent
print("Got frame")
local inputbox = frame:WaitForChild("TextBox")
print("Got textbox")
local value = inputbox:WaitForChild("Value")
local holder = frame.Parent
print("Got admin")
local display = holder:WaitForChild("display")
print("Got display")
local imagelabel = display:WaitForChild("ImageLabel")
print("Got imagelabel")
local serverstorage = game:GetService("ServerStorage")
print("Got serverstorage")
local userimg = 'http://www.roblox.com/Thumbs/Avatar.ashx?format=png&x=100&y=100&username='
print("Got userimg")
local rptext = display:WaitForChild("rptext")
print("Got rptext")
local username = frame:WaitForChild("username")
print("Got username")

script.Parent.MouseButton1Click:connect(function()
    username.Value = value.Value
    print("1")
    wait()
    imagelabel.Image = userimg..username.Value
    print("2")
    wait()
    local rpvalue = serverstorage:findFirstChild("rP-"..value.Value)
    print("3")
    if rpvalue then
        print("rpvalue exists")
    rptext.Text = "rP | "..rpvalue.Value
    else
        print("rpvalue does not exist; assuming user has no profile.")
        rptext.Text = "This user has not created a profile."
        end
end)

When I enter the game and I search my name, it returns "This user has not created a profile."

This entire system works in edit mode, but not in-game. Could someone please explain to me my problem?

Sorry for the messy scripts!

EDIT:

By the way, my rpvalue DOES exist. I checked with a script.

local serverstorage = game:GetService("ServerStorage")

local rpvalue = serverstorage:findFirstChild("rP-Vexture")

if rpvalue then
print("rpvalue exists")
else
print("rpvalue is nil")
end

It returns "rpvalue exists"

So I'm not quite sure what the problem is here. Can localscripts not access serverstorage? Is this the problem?

Answer this question