Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Works in studio, not in game. I do believe I am using the right kind of scripts?

So I have 3 scripts:

LocalScript chatcmds(in StarterPlayerScripts):

repeat
	wait()
until _G.admins
local player = game.Players.LocalPlayer

local DSService = _G.DSService
local GetSaved = _G.admins

function has_value(tab, val)
    for index, value in ipairs (tab) do
        if value == val then
            return true
        end
    end

    return false
end

for _,object in pairs(GetSaved) do
	if has_value(_G.defaultadmins, player.UserId) then
		player.Team = game.Teams.Owner
	else
		player.Team = game.Teams.Admin
	end
end

player.Chatted:connect(function(msg)
	GetSaved = DSService:GetAsync("admins")
	if has_value(GetSaved, player.UserId) then --admin commands
		if has_value(_G.defaultadmins, player.UserId) then --main owner commands
			if msg == ":reseta" then
				print("Admins RESET!")
				DSService:SetAsync("admins", _G.defaultadmins)
				_G.admins = DSService:GetAsync("admins")
			end
			if split(msg, "%s")[1] == ":adda" then
				if split(msg, "%s")[2] then
					print("Admin added!")
					local tempt = GetSaved
					if not has_value(tempt, split(msg, "%s")[2]) then
						table.insert(tempt, split(msg, "%s")[2])
					end
					DSService:SetAsync("admins", tempt)
					_G.admins = DSService:GetAsync("admins")
				end
			end
			if split(msg, "%s")[1] == ":removea" then
				if split(msg, "%s")[2] then
					print("Admin removed!")
					local tempt = GetSaved
					if has_value(tempt, split(msg, "%s")[2]) then
						removekey(tempt, split(msg, "%s")[2])
					end
					DSService:SetAsync("admins", tempt)
					_G.admins = DSService:GetAsync("admins")
				end
			end
		end
		if msg == ":lista" then
			print("Admins:")
			for _,object in pairs(GetSaved) do
				print(object)
			end
		end
		if split(msg, "%s")[1] == ":givem" then
			if split(msg, "%s")[2] and split(msg, "%s")[3] then
				if tonumber(split(msg, "%s")[3]) then
					if game.Players:FindFirstChild(split(msg, "%s")[2]) ~= nil then
						local p2 = game.Players:FindFirstChild(split(msg, "%s")[2])
						p2.leaderstats.Money.Value = p2.leaderstats.Money.Value + split(msg, "%s")[3]
					end
				end
			end
		end
		if split(msg, "%s")[1] == ":takem" then
			if split(msg, "%s")[2] and split(msg, "%s")[3] then
				if game.Players:FindFirstChild(split(msg, "%s")[2]) ~= nil then
					if tonumber(split(msg, "%s")[3]) then
						local p2 = game.Players:FindFirstChild(split(msg, "%s")[2])
						if p2.leaderstats.Money.Value >= tonumber(split(msg, "%s")[3]) then
							p2.leaderstats.Money.Value = p2.leaderstats.Money.Value - split(msg, "%s")[3]
						end
					end
				end
			end
		end	
	end
end)

function removekey(tab, key)
    for _,object in pairs(tab) do
		if object == key then
			table.remove(tab,_)
			break
		end
	end
    return tab
end

function split(pString, pPattern)
   local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pPattern
   local last_end = 1
   local s, e, cap = pString:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
     table.insert(Table,cap)
      end
      last_end = e+1
      s, e, cap = pString:find(fpat, last_end)
   end
   if last_end <= #pString then
      cap = pString:sub(last_end)
      table.insert(Table, cap)
   end
   return Table
end

ServerScript Admins:

local DSService = game:GetService('DataStoreService'):GetDataStore('mydatastorehere')
_G.DSService = DSService
_G.defaultadmins = {98016926}

local GetSaved = DSService:GetAsync("admins")

if not GetSaved then
	DSService:SetAsync("admins", _G.defaultadmins)
end

_G.admins = GetSaved

and server script gamepasses:

game.Players.PlayerAdded:connect(function(p)
	p.CharacterAdded:connect(function(character)
		repeat
			wait()
		until _G.admins
		if game:GetService("GamePassService"):PlayerHasPass(p, 492262492) then --speed
			workspace[p.Name]:WaitForChild("Humanoid").WalkSpeed = 100
		end
		if game:GetService("GamePassService"):PlayerHasPass(p, 496545632) then --vip
			game.ReplicatedStorage.Spatula:Clone().Parent = p.Backpack
		end
		if has_value(_G.admins, p.UserId) then
			game.ReplicatedStorage.Spatula:Clone().Parent = p.Backpack
			workspace[p.Name]:WaitForChild("Humanoid").WalkSpeed = 100
		end
	end)
end)

function has_value(tab, val)
    for index, value in ipairs (tab) do
        if value == val then
            return true
        end
    end

    return false
end

The gamepasses script works, and gives me 2 spatulas(one from the gamepass, one from being owner), but the ChatCmds one doesn't work at all once published. I am being left on the neutral team. Note that mydatastorehere is not really my data store, I just wanted to make sure no one who reads this messes with the one I have. Can someone please tell me why I am not being put as owner(I am in testing mode)?

Doing a RemoteEvent like SwardGames said, I added this in the admin script:

local geta = Instance.new("RemoteEvent") -- creates the event object
geta.Parent = game.Workspace -- puts the event into the Workspace
geta.Name = "GetAsync" -- giving a name to the event so we can fire it specifically elsewhere
geta.OnServerEvent:connect(function(wtg) -- define the function that will be called when the event is fired
    return DSService:GetAsync(wtg)
end)

However neither ChatCmds or Gamepasses can get past:

repeat
	wait()
until workspace.GetAsync:FireServer("admins")

It just waits forever.

1 answer

✓ Accepted answer

I believe (albeit from a quick glimpse) that you have fallen into the _G trap: _G on the server is separate from _G on each client.

9 comments

0

Ahh yes! So how can I get the DataStoreService in my localscript? I forget why putting that line of code in didn't work, but I think it was because its a localscript.

0

You can't (if you could players could add/delete/change anything at will). You could use a RemoteFunction/RemoteEvent to contact the server and have the server save.

0

An ObjectValue should be able to hold the DataStoreService though, right?

0

No, because it wont replicate. It would be extremely bad design to allow a client to have the ability so save and write persistant data to the server.

0

Ok, do you mind giving me an example of how to do it? Because I never could get Remote Functions to work.

0

In the remote event, they have 2 arguments, player and arguments. But when the function is called, its only called with the arguments?

0

Please read the edit I made to the main post when you get a chance.

0

Use a RemoteFunction, as RemoteEvents cannot return anything. Also, the first argument of OnServerInvoke is the player who called it, IE: myRemoteFunction.OnServerInvoke = function(player, text) print("Player " .. player.Name .. " called my RemoteFunction to tell it " .. text) end

This discussion is preserved as it was. New answers, comments, and votes are closed.