You can write your topic however you want, but you need to answer these questions:
What do you want to achieve?
I am trying to make a scale that show’s you how much weight you have, i have already done this but I don’t know how to make the function to do the action only once when the player touches the part not a hundered times, I also want to make that when the player steps off and back on the scale the function does the action again
Here is a clip:
i have tried searching the problem on the developer hub there were answers but I didn’t quite understand it so I made a poll talking about the EXACT problem I am having.
It is not looping, what happens here is -
You need to have a debounce, because currently it fires for every thing that touches the brick.
If you want only the player to be able to make it happen, simply add an if statement to check if it has a humanoid inside it [the parent of the part that touched], and check if he’s indeed a player.
Humanoid:FindFirstAncestorWhichIsA(“Model”) should allow you to find the player’s character, and you can check if he’s a player by looking for the character name in the Player’s Service.
So your code should look something like this:
local char = hit:FindFirstAncestorWhichIsA("Model")
if char:FindFirstChildWhichIsA("Humanoid") then
if game.Players:FindFirstChild(char.Name) then
-- What you want to do in here
end
end
Also, make sure to include a debounce in the function that detects the collision as the function can run multiple times from one actual “touch” due to character accessories and body parts.
This should be correct but it isn’t working, unless the debounce needs to be somewhere else in the script or it is some other issue that I am not seeing.
local scalekilos = game.Workspace.Part.SurfaceGui.TextLabel
local areyoufat = game.Workspace.Part.SurfaceGui.fat
local debounce = false
script.Parent.Touched:Connect(function(scale)
local char = script.Parent:FindFirstAncestorWhichIsA("Model")
if char:FindFirstChild(char.name) then
print(scale.Mass)
if scale.Mass <= 0.5 and not debounce then
scalekilos.Text = scale.Mass .. " kg"
areyoufat.Text = "You are not fat"
debounce = true
task.wait(3)
debounce = false
else if scale.Mass >= 0.5 and not debounce then
scalekilos.Text = scale.Mass .. " kg"
areyoufat.Text = "You are fat"
debounce = true
task.wait(3)
debounce = false
end
end
end)
local scalekilos = game.Workspace.Part.SurfaceGui.TextLabel
local areyoufat = game.Workspace.Part.SurfaceGui.fat
local debounce = false
script.Parent.Touched:Connect(function(scale)
if scale.Mass <= 0.5 and not debounce then
print(scale.Mass)
scalekilos.Text = scale.Mass .. " kg"
areyoufat.Text = "You are not fat"
debounce = true
task.wait(3)
debounce = false
else if scale.Mass >= 0.5 and not debounce then
print(scale.Mass)
scalekilos.Text = scale.Mass .. " kg"
areyoufat.Text = "You are fat"
debounce = true
task.wait(3)
debounce = false
end
end
end)
local scalekilos = game.Workspace.Part.SurfaceGui.TextLabel
local areyoufat = game.Workspace.Part.f.fat
local debounce = false
script.Parent.Touched:Connect(function(scale)
local humanoid = scale.Parent:FindFirstChild("Humanoid")
if not humanoid then return end
local roundedMass = math.round(scale.Mass) -- Optional for rounding the number
if roundedMass <= 0.5 and not debounce then
print(roundedMass)
scalekilos.Text = roundedMass .. " kg"
areyoufat.Text = "You are not fat"
debounce = true
task.wait(3)
debounce = false
else if roundedMass >= 0.5 and not debounce then
print(roundedMass)
scalekilos.Text = roundedMass .. " kg"
areyoufat.Text = "You are fat"
debounce = true
task.wait(3)
debounce = false
end
end
end)
Your function isn’t looping, the BasePart.Touched() event runs everytime any parts collide. To stop functions from running over and over again one of the best solutions is a debounce
Do note I have not tested this code as I havent made the testing area but i dont see why this code wouldnt work
local debounce = false
local part = **PATH TO YOUR PART HERE**
local player = nil
part.Touched:Connect(function(obj)
if debounce == true then return end
local plr = game.Players:GetPlayerFromCharacter(obj.Parent) -- Returns the Player instance if obj.Parent is set as the `Character`
if plr ~= nil then
debounce = true
local char = plr.Character
player = plr
-- Your code here
end
end)
part.TouchEnded:Connect(function(obj)
if debounce == false then return end
local plr = game.Players:GetPlayerFromCharacter(obj.Parent)
if plr == player then
debounce = false
end
end)
local Script = script
local Workspace = workspace
local Part = Script.Parent
local GuiPart = Workspace.Part
local Gui = GuiPart.SurfaceGui
local Scale = Gui.TextLabel
local Fat = Gui.fat
local Debounce = false
Part.Touched:Connect(function(OtherPart)
if Debounce then return end
Debounce = true
Scale.Text = OtherPart.Mass.." KG"
Fat.Text = if OtherPart.Mass > 0.5 then "You are fat." else "You are not fat."
task.wait(3)
Debounce = false
end)