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

How can I send this to Client or fix this script correctly?

Problem

I'm trying to make a level up System where if the players XP is higher than Excap it will subtract from Excap's value. All this works in studio but not in game. PlayerAdded can only work in Server Script. And FireClient isn't working. Unless I did something wrong. Can I get some help?

Server Script

game.Players.PlayerAdded:connect(function(Player) 
	local Data = Instance.new("IntValue") 
	Data.Name = "Data" 
Data.Parent = Player
    local XP = Instance.new("IntValue") 
	XP.Name = "XP" 
	XP.Value = 0
XP.Parent = Data
    local Level = Instance.new("IntValue") 
	Level.Name = "Level"
	Level.Value = 1 
Level.Parent = Data
	local Tries = Instance.new("IntValue")
	Tries.Value = 10
	Tries.Name = "Tries"
Tries.Parent = Data
	local ExCap = Instance.new("IntValue")
	ExCap.Value = 500
	ExCap.Name = "ExCap"
ExCap.Parent = Data
	XP.Changed:Connect(function() XPChange(Player,XP,Level,ExCap) end) 
end)


function XPChange(Player, XP, Level, ExCap)
	game.ReplicatedStorage.RemoteEvent:FireClient(Player, XP,Level,ExCap)
    end

Local Script

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(XP,Level,ExCap)
	    if XP.Value >= ExCap.Value then
        if Level.Value < 100 then
            Level.Value = Level.Value + 1;
            XP.Value = XP.Value-ExCap.Value;
            ExCap.Value = ExCap.Value * 2;
        end
    end
    if Level.Value == 100 and XP.Value ~= 0 then
        XP.Value = 0;
end
end)

4 comments

0

connect is deprecated. Use Connect instead.

0

im not super sure what is going on cuz im not that good, but i think u should change XP.Changed blah blah blah to XP.Changed:Connect(XPChange)

0

it's XP.Value.Changed (you forgot the .Value, thus checking for the instance rather than the XP value)

0

(also put the XPChange function as a local function at the top of the script because you can't call it before it gets initialized)

1 answer

Read this: You should learn more about instance.new, leaderstats, datastore remoteevents... If you just copy and paste this and dont learn about it you will dont understand anything about this

You should not use remote here cuz game.Players is server-sided so you can change things in the players without localscript.

i'll will try my best to guide you through this.(sry for my bad english ;P xP)

I dont know what you want "Tries" for and i dont see it beeing used in the script so i'll skip it.

At the beginning you should create a datastore for each instance.new

xps = game:GetService("DataStoreService"):GetDataStore("xps") -- This will store the xp
levels = game:GetService("DataStoreService"):GetDataStore("levels") -- This will store the level
Excap = game:GetService("DataStoreService"):GetDataStore("Excap") -- The amount of xp needed

I wont explain this part much cuz you should now this: I'll explain GetAsync later

game.Players.PlayerAdded:Connect(function(player)
	stats = Instance.new("Folder", player) -- Lets make a leaderstats. It will be easier to see if it works then.
	stats.Name = "leaderstats"
	
	xp = Instance.new("IntValue", leaderstats)
	xp.Name = "XP"
	xp.Value = xps:GetAsync(player.UserId) or 0

	level = Instance.new("IntValue", leaderstats)
	level.Name = "Level"
	level.Value = levels:GetAsync(player.UserId) or 1

	excap = Instance.new("IntValue", leaderstats)
	excap.Name = "Excap"
	excap.Value = excaps:GetAsync(player.UserId) or 500

end)

Now lets save the data if the values change This will be inside the game.Players.PlayerAdded:Connect(function(player) Set/GetAsync uses to set or load data. in this case we set the data to the players userid and the data we will set is xp,level and excap. The GetAsync abow will get the data from the player who joined. If the player doesnt have a data it will be set to the thing after or

	xp.Value.Changed:Connect(function()
		xps:SetAsync(player.UserId, xp.Value)
	end)
	
	level.Value.Changed:Connect(function()
		levels:SetAsync(player.UserId, level.Value)
	end)
	
	excap.Value.Changed:Connect(function()
		excaps:SetAsync(player.UserId, excap.Value)
	end)
	

and now lets add your calculations inside the game.Players.PlayerAdded:Connect(function(player) aswell. We dont need these ;

        if xp.Value >= excap.Value then
        if level.Value < 100 then
            level.Value = level.Value + 1
            xp.Value = xp.Value-excap.Value
           excap.Value = excap.Value * 2
        end
    end
    if level.Value == 100 and xp.Value ~= 0 then
        xp.Value = 0
end

So in the end it will be something like this

xps = game:GetService("DataStoreService"):GetDataStore("xps")
levels = game:GetService("DataStoreService"):GetDataStore("levels")
excaps = game:GetService("DataStoreService"):GetDataStore("Excaps")

game.Players.PlayerAdded:Connect(function(player)
	stats = Instance.new("Folder", player)
	stats.Name = "leaderstats"
	
	xp = Instance.new("IntValue", leaderstats)
	xp.Name = "XP"
	xp.Value = xps:GetAsync(player.UserId) or 0
	
	level = Instance.new("IntValue", leaderstats)
	level.Name = "Level"
	level.Value = levels:GetAsync(player.UserId) or 1
	
	excap = Instance.new("IntValue", leaderstats)
	excap.Name = "Excap"
	excap.Value = excaps:GetAsync(player.UserId) or 500
	
	xp.Value.Changed:Connect(function()
		xps:SetAsync(player.UserId, xp.Value)
	end)
	
	level.ValueChanged:Connect(function()
		levels:SetAsync(player.UserId, level.Value)
	end)
	
	excap.ValueChanged:Connect(function()
		excaps:SetAsync(player.UserId, excap.Value)
	end)
	
	
        if xp.Value >= excap.Value then
        if level.Value < 100 then
            level.Value = level.Value + 1
            xp.Value = xp.Value-excap.Value
           excap.Value = excap.Value * 2
        end
    end
    if level.Value == 100 and xp.Value ~= 0 then
        xp.Value = 0
end

	
end)

5 comments

0

Does this work with FE?

1

oh god this is such a trainwreck, first of all you dont need 3 datastores ESPECIALLY for such trivial things as integers (1 datastore that stores a table with the values inside is enough) and, for the love of god DO NOT SAVE every time the value changes as that will throttle the datastore request limits.

0

GGButNO_RE yes it does :) and raduaavin366 everyone has their own way :) ofcourse you can do this in 10000 ways and this way still works

0

"You should learn about Instance.new" > gives deprecated code. YOU should learn. You should also be encouraging the BEST practices, not the worst. And game.Players is NOT server-sided.

0

Cant understand why you are mad. I helped as much as i could if you want to answere in a bettee way fine but you dont have to be mad. And i dont think it exist a best way cuz every way has their own benefits

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