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

Conditional statement refuses to work. How can I make it work? >;c

Asked by 8 years ago

I'm absolutely horrible at debugging, so I'm sorry if the fix is an annoyingly easy. I made a script that's /supposed/ to check if the player whose data I'm editing is in the game so it can edit the in-game value too-- and if they aren't there just normally change the data.. but each time I enter someone who isn't in the game it checks for them anyway and then tells me "so and so is not a valid member of players"

I've rewritten this same script a bunch of times but whatever I tried gives the same output.

local PDS = game:GetService("DataStoreService"):GetOrderedDataStore("NumberOfPosts")

local UserTB = script.Parent.Parent.UserTB
local ValueTB = script.Parent.Parent.ValueTB

script.Parent.MouseButton1Click:connect (function()
    if game.Players[UserTB.Text] then
        local key = UserTB.Text 
        local S = tonumber(ValueTB.Text)
        game.Players[UserTB.Text].leaderstats.Posts.Value = tonumber(ValueTB.Text)
        PDS:SetAsync (key, S)
        print("Value for " .. UserTB.Text .. " has been changed to" .. ValueTB.Text)
    else
        local key = UserTB.Text 
        local S = tonumber(ValueTB.Text)
        PDS:SetAsync (key, S)
        print("Value for " .. UserTB.Text .. " has been changed to" .. ValueTB.Text)
    end
end)

could you tell me how I can fix this and how I can avoid this problem in the future? D;

0
I may be wrong, but i spotted a space on line 6, with the function. I dont think there should be a space. ConnorVIII 448 — 8y

1 answer

Log in to vote
0
Answered by
Wutras 294 Moderation Voter
8 years ago

There are only a few problems i have detected, but I'm not sure whether those are the problems you're talking about. I'll show those problems in the script and explain how to solve them.

local PDS = game:GetService("DataStoreService"):GetOrderedDataStore("NumberOfPosts")

local UserTB = script.Parent.Parent.UserTB
local ValueTB = script.Parent.Parent.ValueTB

script.Parent.MouseButton1Click:connect (function() -- There's no need for a space after connect, it might even crash the script.
    if game.Players[UserTB.Text] then -- This only checks for the server you're on, so you may need something like a DataStore or a Website combined with HttpService. I'd also propose to just change the if statement to: "if game.Players:WaitForChild(UserTB.Text) then".
        local key = UserTB.Text 
        local S = tonumber(ValueTB.Text)
        game.Players[UserTB.Text].leaderstats.Posts.Value = tonumber(ValueTB.Text) -- Same thing over here!
        PDS:SetAsync (key, S)
        print("Value for " .. UserTB.Text .. " has been changed to" .. ValueTB.Text)
    else
        local key = UserTB.Text 
        local S = tonumber(ValueTB.Text)
        PDS:SetAsync (key, S)
        print("Value for " .. UserTB.Text .. " has been changed to" .. ValueTB.Text)
    --I guess that the second variant - the block after else - is the better version and you just have to make the server update the player's stats like every few minutes or so.
    end
end)

Besides those named problems there were no other that I found now. I hope it helps!

Ad

Answer this question