Having a problem with my roblox script

Hi, Scripters!

I recently ran into a problem while making a script. It allows a character to click on one object at a time and wait until that object is broken to select another one. The problem I’m facing is that everyone on the server is set to these boundaries and I want it to be for the local player.

I currently have an Int Value in the StarterPlayer. This is the section of the code where once the player clicks the object, if ClickedObjects is less than 1, then the player can click the object to break it.

If anyone knows how to make it so this constraint of one person clicking at a time can be fixed so everyone can have one click at a time to themselves, I would really appreciate the help. Thanks!

Screen Shot 2021-12-16 at 8.56.11 PM

local function onClick(Player)
	ClickedObjects.Value += 1
	print("ClickedObjects is currently at ".. ClickedObjects.Value.. " item(s)")
	if ClickedObjects.Value > 1 then
		error_sound:Play()
		ClickedObjects.Value -= 1
	elseif ClickedObjects.Value <= 1 then
		Tree.ClickDetector:Destroy()
...

Feel free to ask any questions about it if you are confused! I’m ready to reply :slight_smile:

Is this a local or server script?

And can you show where you defined the variables?

This is a server script inside the object being broken. Here’s the whole script:

local GUI = script.Parent.Health
local Tree = script.Parent
local Leaves = Tree.Leaves:GetChildren()
local hit_sound = Tree.Hit
local break_sound = Tree.WoodBreak
local PlayerPower = game.ReplicatedStorage.PlayerPower.Value
PlayerPower = 1 --Test
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Treepos = Tree.Position
local ServerStorage = game:GetService('ServerStorage')
local Health = script.Parent.HealthValue.Value
Health = 10
local OriginalHealth = 10
local ClickedObjects = game.StarterPlayer.ClickedObjects
local error_sound = Tree.Errorsound

local function onClick(Player)
	ClickedObjects.Value += 1
	print("ClickedObjects is currently at ".. ClickedObjects.Value.. " item(s)")
	if ClickedObjects.Value > 1 then
		error_sound:Play()
		ClickedObjects.Value -= 1
	elseif ClickedObjects.Value <= 1 then
		Tree.ClickDetector:Destroy()
		while true do
			hit_sound:Play()

			Health -= PlayerPower

			GUI.Green.Size = UDim2.new(Health/10, 0, 0.5, 0)
			script.Parent.Health.TextLabel.Text = Health.. "/10"

			if Health <= 0 then
				ClickedObjects.Value -= 1
				Player.leaderstats.Materials.Value += OriginalHealth
				GUI.Active = false
				for i, leaf in pairs(Leaves) do
					leaf.Transparency = 0.2
					leaf.Anchored = false
					leaf.CanCollide = false
				end
				wait(1)
				Tree:Destroy()
				break_sound:Play()
				wait(1)
				local NewTree = game.ServerStorage.Tree1:Clone()
				NewTree.Parent = game.Workspace
				NewTree.Position = Treepos
				wait()
				print("Respawned Tree!")
				break
			end
			wait(0.5)
			print("Clicked!")
		end
	end
end



Tree.ClickDetector.MouseClick:Connect(onClick)


I’m assuming this is a server-script, instead of referencing the StarterPlayer value, reference the value inside the Player instance, like this:

local function onClick(Player)
    ClickedObjects = player:FindFirstChild("ClickedObjects")

	ClickedObjects.Value += 1
	print("ClickedObjects is currently at ".. ClickedObjects.Value.. " item(s)")
	if ClickedObjects.Value > 1 then
		error_sound:Play()
		ClickedObjects.Value -= 1
	elseif ClickedObjects.Value <= 1 then
		Tree.ClickDetector:Destroy()
...

Oh, so I should make its parent the local player?

Anything inside StarterPlayer will automatically be parented to the PlayerScripts folder, providing it’s not the CharacterScripts folder


When I’m in-game, this is what I see in the explorer. Is the ClickedObjects supposed to be inside the player scripts just as an int value by itself?

You’d need to put it under the StarterPlayerScripts and then reference it inside the PlayerScripts folder

Ok, let me try that out. I’ll get back to you in a little bit about it

local ClickedObjects = game.StarterPlayer.ClickedObjects

The starter player is inside of the players

local ClickedObjects = game:GetService("Players")["Players name"].PlayerScripts.ClickedObjects

Assumming the ClickedObjects is in starterPlayerScripts ^

This is how you could do it in a server script, I also suggest that instead of putting a server script in all things being deleted you make one local script in starterPlayers or starterGui and put the ClickedObjects inside of that.

Also the tree and click detector are being deleted for the whole server.

You should use

task.wait(.5)

looking at this function you should do Plr.ClickedObjects.Value and not

StarterPlayer.ClickedObjects.Value
and also putting an object in starterPlayer wont really parent it to the player instead i recommend you to add it via a script here is an example

game.Players.PlayerAdded:Connect(function(plr)
local clickedObject = Instance.new("NumberValue")
clickedObject.Parent = plr
clickedObject.Name = "ClickedObjects"
end)

then here is your script for clicking

local function onClick(Player)
	Player.ClickedObjects.Value += 1
	print("ClickedObjects is currently at ".. Player.ClickedObjects.Value.. " item(s)")
	if Player.ClickedObjects.Value > 1 then
		error_sound:Play()
		ClickedObjects.Value -= 1
	elseif Player.ClickedObjects.Value <= 1 then
		Tree.ClickDetector:Destroy()

Then what would I set ClickedObjects to in my main script that’s inside the object? Because I just added a new script to ServerScriptService for adding the NumberValue to the player

if your clickedObject is a variable like this local ClickedObject = game.StarterPlayer.ClickObject then you should remove it as plr.ClickObject is actually getting the numberValue (clickedObject) from the player which is best approach in my opinion

So right now, local ClickedObjectis still outside of the local function. Do I need to put it in the function? If so, how should I arrange it.

Edit: This is what my ClickedObject is set to:

local ClickedObjects = game.Players.LocalPlayer:WaitForChild("ClickedObjects")

local ClickedObjects = game.Players.LocalPlayer:WaitForChild("ClickedObjects")

is that a local script? as game.Players.LocalPlayer doesnt work on a script it only works on local script and also plr.ClickedObjects is already doing the work so i dont think its good to put the local clickedObjects

1 Like

This is all in a Server script. So what would you suggest that I replace with ClickedObjects in this case?

1 Like

Finally got it working! Thanks to you and the people that helped as well.

1 Like