Help With Exp When You Touch a Part

So basically i’m trying to make a exp. and level system I want want it so that when you touch this certain part, you exp from it.

I’ve added print statements and testing it, but like the whole script just fails. I also asked someone to help me see if there were any problems. (two pairs of eyes are better than one :slight_smile:)

I would really appreciate it if someone could help me with this. Thanks for reading :wave::


Could you show us the script you used?

Not sure if you tried inserting your code or not but if you can explain how you’re storing your experience values I can offer my help.

Sure thing!

game.Players.PlayerAdded:Connect(function(plr)
local a = Instance.new(“Folder”,plr)
a.Name = “leaderstats”
local x = Instance.new(“IntValue”,a)
x.Value = 0
x.Name = “exp”
end)

while true do

end
local BlckLndsEndZne = game.Lighting.Map1.BlockLandsEndZone
print(“done line 1”)

workspace.BlckLndsEndZne.Touched:connect(function(hit)
print(“done line 15”)
if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then
print(“done line 17”)
local exp = game.ServerScriptService.experiencepoints
print(“done line 19”)
exp = exp +25
print(“done line 21”)
exp.Position = hit.Position
print(“done line 23”)
exp.Parent = workspace
print(“done line 25”)
end
end)

local exp = game.StarterGui.ExpAndLvl.ExpPoints
local lvl = game.StarterGui.ExpAndLvl.PlrLevel
local win = game.ServerScriptService.LevelLeaderBoard

if exp.Value == 25 then
lvl = lvl +1
print(“You reached level 1!”)
end

What does this mean exactly? Are there errors?

One thing I notice instantly is that you don’t use a debounce, meaning the script will run multiple times on a touch.

Yeah but even if just stand on the part forever, it still wouldn’t give me any points.

This is going to cause a game script timeout. It’s probably why the whole script just fails. It’s empty anyway, so you should just remove that section.

It’s because the script stopped executing due to that error already.

Oh ok thanks! I’ll test that. (30 char)

A few other things I noticed with your script:

  1. You use :connect which is deprecated, so you should use :Connect instead.
  2. You don’t have a debounce on the touched function, which means it will fire more than 1 time when you’re standing on the part.
  3. You tried to set the value of a gui object using game.StarterGui instead of player.PlayerGui, which is what you should be using instead.
  4. Also, please indent. It’ll make debugging easier for you and also make it easier for us to find errors for you. :slight_smile:
2 Likes
local Players = game:GetService("Players")

local function newLeaderStatsFolder(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name ="leaderstats"
    leaderstats.Parent = player

    local experience = Instance.new("IntValue")
    experience.Name = "Experience"
    experience.Value = 0 --If you have a datastore then this would be where to connect the DataStore value
    experience.Parent = leaderstats

    local level = Instance.new("IntValue")
    level.Name = "Level"
    level.Value = 0 --DataStore save would go here
    level.Parent = leaderstats
end

local function determineLevelUp(player)
    if Players.player.leaderstats.Experience = 100 then
        Players.player.leaderstats.Level = Players.player.leaderstats.Level + 1
        Players.player.leaderstats.Experience = 0
    end
end

local function addExperienceToPlayer(hit)
    local debouce = false
    if hit.Parent:FindFirstChild("Humanoid") then
      if Players:GetPlayerFromCharacter(hit) then
          if not debounce then
              debounce = true
              Players.hit.leaderstats.Value = Players.hit.leaderstats.Experience.Value + 25
              determineLevelUp(Players:GetPlayerFromCharacter(hit))
              wait(10)
               debounce = false
          end
      end
    end
end

Players.PlayerAdded:Connect(newLeaderStatsFolder)
game.Workspace.BlckLndsEndZne.Touched:Connect(addExperienceToPlayer)
2 Likes

Above should be marked as the solution.
Also, to the OP: This should be a script in ServerScriptService.

1 Like

I have it in ServerScriptService.

Hmm… For some reason it doesn’t work…

I didn’t make it work 100% with your script so you may need to add things to it.

Ahh ok. Thanks though! (30 char)

I mainly left out the GUI bit since I already had used leaderstats. But I think the code should work fine otherwise.

@Kamlkaze_Kid

Don’t use the second argument for Instance.new(), there was a thread in 2016
that addressed this.

Second,

what is the purpose of this empty while loop??

Try to use descriptive variables so that it is easy to catch errors, instead of defining a folder as x, define it as leaderstats = etc.

Do not attempt to use Lighting for storing instances, this usage is legacy like and use either ReplicatedStorage or ServerStorage to store items instead .

This code is outside the function bound to the PlayerAdded event, it would error if workspace.BlckLndsEndZne did not exist.

Make sure this is a script then, local scripts can’t access ServerScriptService as it’s contents are not replicated.

Also you are attempting to add an instance to 25, you meant to do something like this instead:

   local exp = something.Points
   exp.Value = exp.Value + 25

Now I am not sure what exp is at all if it has a supposed Position property.

What? game.StarterGui’s contents are replicated into a player’s PlayerGui, access that by doing

    local PlayerGui = player:WaitForChild("PlayerGui", 5) 
    -- returns nil if the instance wasn't found within 5 seconds
  • Implement some sort of debounce mechanism.
  • Use :Connect (capital C), connect is deprecated.

I don’t think any print statement at all will print.

Please, format code; it’ll be better for us (and you) to debug.

Also, don’t store things in Lighting. Lighting is for lighting. Store things you want to be replicated in either ReplicatedStorage or ReplicatedFirst.

Edit: My bad, I didn’t see that this was included in the above reply.