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

What is wrong with the script that is supposed to give armor when players press a button?

so can someone tell me whats wrong with this, its a script that is supposed to send a request to the server to check the players gold to see if its high enough to buy the armor, and if it is, then it clones it, if not, it should do nothing

--Server:
local players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local blackevent = rs.armors.ArmorEvents:WaitForChild("BlackEvent")
local goldevent = rs.armors.ArmorEvents:WaitForChild("GoldEVENT")
local regevent = rs.armors.ArmorEvents:WaitForChild("RegEvent")
function onBlackInvoke(plr)
	print("epic")
	if plr.leaderstats.Gold.Value < 1500 then
		print("epic")
		return false
	elseif plr.leaderstats.Gold.Value >= 1500 then
		print("epic")
		plr.leaderstats.Gold.Value = plr.leaderstats.Gold.Value - 1500
		return true
	end
end
function onGoldInvoke(plr)
	print("epic")
	if plr.leaderstats.Gold.Value < 1000 then
		print("epic")
		return false
	elseif plr.leaderstats.Gold.Value >= 1000 then
		print("epic")
		plr.leaderstats.Gold.Value = plr.leaderstats.Gold.Value - 1000
		return true
	end
end
function onRegInvoke(plr)
	print("epic")
	if plr.leaderstats.Gold.Value < 500 then
		print("epic")
		return false
	elseif plr.leaderstats.Gold.Value >= 500 then
		print("epic")
		plr.leaderstats.Gold.Value = plr.leaderstats.Gold.Value - 500
		return true
	end
end
blackevent.OnServerInvoke = onBlackInvoke()
goldevent.OnServerInvoke = onGoldInvoke()
regevent.OnServerInvoke = onRegInvoke()


-- Client:
interface.ScrollingFrame.BlackBuy.Activated:connect(function()
	if blackevent:InvokeServer() == true then
		rs.armors["Dark Armor (+250"]:Clone().Parent = client.Backpack
	elseif
		blackevent:InvokeServer() == false then
			return
	end
end)
interface.ScrollingFrame.GoldBuy.Activated:connect(function()
	if goldevent:InvokeServer() == true then
		rs.armors["Golden Armor (+150)"]:Clone().Parent = client.Backpack
	elseif goldevent:InvokeServer() == false then
		return
	end
end)
interface.ScrollFrame.RegBuy.Activated:connect(function()
	if regevent:InvokeServer() == true then
		rs.armors["Armor (+75)"]:Clone().Parent = client.Backpack
	elseif regevent:InvokeServer() == false then
		return
	end
end)
-- Not all of client script, but the part where its used.

1 comment

1

For the server script, try taking "()" away from line 40 - 43, also, In the future try experimenting with tables rather that copy and pasting the same code for different items. They are very helpful.

1 answer

✓ Accepted answer

First, Stephenthefox is right. That is the issue. However I would like to go in depth with the solution.

What you are doing currently is:

blackevent.OnServerInvoke = true -- or false if the function return false
-- And so on.

When you do var = func(...), you are calling the function func and assigning var the return values of func. Not all functions have return values, but in your case they did. You were not assigning OnServerInvoke the function itself, but the return values. What you are looking for is this:

blackevent.OnServerInvoke = onBlackInvoke

This will assign OnServerInvoke the function and not the return values.

var = func will assign var to the function func, and not its return values. The function isn't called either.

Additionally, I suggest instead of having 3 remote functions, have one. And from the client request black or gold.

local function onInvoke(client, request) -- localise your variables.
    -- Do stuff. 'request' (name it whatever you want) will be what the client requests. This will be the black or gold or reg.
end

And from a client: (This is just an example with the reg)

interface.ScrollFrame.RegBuy.Activated:Connect(function() -- connect is deprecated, use Connect
    if regevent:InvokeServer("Reg") then -- == true not needed, and you send requested item to the server.

        rs.armors["Armor (+75)"]:Clone().Parent = client.Backpack -- do this on server

    else -- You don't need a long elseif, this will execute if first condition isn't met
        return
    end
end)

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