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!
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()
...
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()
...
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.
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
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
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