Hello all, I am currently working on a king of the hill game (KOTH) and I cannot find tutorials on how to script it. What I would like to achieve is a cylinder 0.2 studs above the ground, when you step on it I would like it to give you points for every second that you stand on it.
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local chr = hit.Parent
local plr = game.Players:GetPlayerFromCharacter(chr)
while wait(1) do
plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 1
end
end
end)
I would highly recommend not using touched, due to it having a debounce and a chance not to trigger
Also that script would make it where just by touching, they would gain points indefinitely
Here is my suggestion, you should use magnitude
Simply insert a script into the cylinder.
local hill = script.Parent
local range = tonumber(script.Parent.Size.Y)
while true do -- A loop that fires every second
for i,v in ipairs(game.Players:GetPlayers()) do -- This goes through all the players
if v.Character ~= nil and v.Character:FindFirstChild("Torso") then -- Checks to make sure the players are spawned in and finds the torso. Upper or Lower torso for R15
if (v.Character:FindFirstChild("Torso").Position - hill.Position).Magnitude <= range then --Check to make sure it is within range
v:FindFirstChild('leaderstats'):FindFirstChild("CoinNameHere").Value = v:FindFirstChild('leaderstats'):FindFirstChild("CoinNameHere").Value+ 1 -- Adds points
end
end
end
wait(1)
end
If you plan on making a more complicated game, then instead of just learning for this particular use, I would delve into Part.Touched and also learn how to configure values (that could be placed inside ReplicatedStorage) depending on what’s happening while the Part is touched. For now 4Ive’s script seems like an okay idea, but make sure that you first create the leaderstat. More information how how to do that can be found here. Later on, I personally stop using leaderstats all together and instead create player folders for each player and in there store a value, since it’s much more easy to configure in my opinion. Most people disagree though.